Guarded Type Promotion: Eliminating Redundant Casts in Java

Research output: Contribution to book/anthology/report/proceedingArticle in proceedingsResearchpeer-review

  • Johnni Winther, Denmark
In Java, explicit casts are ubiquitous since they bridge the gap between compile-time and runtime type safety. Since casts potentially throw a ClassCastException, many programmers use a defensive programming style of guarded casts. In this programming style casts are protected by a preceding conditional using the instanceof operator and thus the cast type is redundantly mentioned twice. We propose a new typing rule for Java called Guarded Type Promotion aimed at eliminating the need for the explicit casts when guarded. This new typing rule is backward compatible and has been fully implemented in a Java 6 compiler. Through our extensive testing of real-life code we show that guarded casts account for approximately one fourth of all casts and that Guarded Type Promotion can eliminate the need for 95 percent of these guarded casts.
Original languageEnglish
Title of host publicationProceedings of the 13th Workshop on Formal Techniues for Java-Like Programs
Number of pages8
PublisherAssociation for Computing Machinery
Publication year2011
ISBN (print)978-1-4503-0893-9
Publication statusPublished - 2011
EventFormal Techniques for Java-Like Programs - Lancaster, United Kingdom
Duration: 26 Jul 201126 Jul 2011
Conference number: 13


WorkshopFormal Techniques for Java-Like Programs
LandUnited Kingdom

    Research areas

  • Java, type cast, type checking

See relations at Aarhus University Citationformats

ID: 41953345