I generally view the presence of the instanceof operator in Java code as a “red flag,” meaning that it’s not necessarily wrong to use
instanceof in certain situations, but its use sometimes indicates a design issue that could be resolved in a cleaner way as described in some resources referenced at the end of this post (including resources about similar type checking functionality in languages other than Java).
Although I’ve seen
instanceof used several times when it does not need to be, I’ve run into even more situations where it was not easy to avoid
instanceof. This is particularly true when working with legacy code bases and certain libraries and frameworks in which I have no ability to refactor relationships between classes to support interfaces, method overriding, and other tactics that can be used to remove the need for
A very common technique employed with
instanceof is to immediately cast to the type checked in the conditional using
instanceof. JEP 305 [“Pattern Matching for instanceof (Preview)”] provides an example of this common pattern and I’ve slightly adapted that example here:
Benji Weber has posted on using reflection and on using lambda expressions to achieve Kotlin-like “instanceof smart casts.” Fortunately, JDK 14 and JEP 305 bring built-in language support (albeit preview status) for this approach.
JDK 14 introduces a preview feature that allows the
instanceof conditional and associated cast to be implemented completely within the conditional. The effect on the above code example is shown next:
This preview feature is available in the JDK 14 Early Access Builds and I’m using JDK 14 Early Access Build 34 for my examples in this post.
The JEP 305 preview feature in JDK 14 is a small nicety whose advantage is more obvious in lengthy
else conditional statements. The next two code listings provide a comparison of the “old way” of calling
instanceof and explicitly casting to the “new preview way” of using
instanceof pattern matching.
instanceof Coupled with Explicit Cast
JDK 14/JEP 305 Preview Feature
The full code is on GitHub and the difference between the old approach and new preview approach is available.
instanceof pattern matching is a preview feature, the code using this feature must be compiled with the javac flags
-source 14. It must be executed with java flag
For more details on how this feature is implemented, see the post “RFR: JDK-8237528: Inefficient compilation of Pattern Matching for instanceof.” Pattern matching support for
instanceof is another Amber-provided step toward reduced boilerplate code in Java.
Resources on Issues Using
- Java ‘instanceOf’: Why And How To Avoid It In Code
- Do you really need instanceof?
- Is using instanceof in Java consider bad practice?
- Using Instanceof Is Mostly a Code Smell
- Instanceof in Conditionals: A Code Smell
- Beware of instanceof Operator
- How Evil is ‘instanceof’?
- Type check is a code smell
Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: JDK 14/JEP 305 instanceof Pattern Matching “Smart Casts”
Opinions expressed by Java Code Geeks contributors are their own.