The instanceOf
operator in Java is used to test whether an object is of a specific type.
A common usage pattern is to first check, then cast and finally use the extracted variable.
In this post we’re going to look at the pattern matching capabilities added to the instanceOf
operator in Java 14 and how it’s going to enhance the programming language.
instanceOf
before Java 14
A typical usage for instanceOf
is the check-then-cast pattern.
if (obj instanceof String) {
var s = (String) obj;
System.out.println(s.toUpperCase());
}
First we check whether obj
is a String
and then we cast obj
into a String
.
These steps go together so often that one might label this as boilerplate.
Next, we’re going to look at how pattern matching tries to provide a better solution.
Pattern Matching in Java 14
Starting from java 14, we can use instanceOf
as follows.
if (obj instanceof String s) {
System.out.println(s.toUpperCase());
}
Casting is done at the same time as the instanceOf
check.
To be technically correct, JEP 305 describes this as a type test pattern.
A pattern is a combination of (1) a predicate that can be applied to a target, and (2) a set of binding variables that are extracted from the target only if the predicate successfully applies to it. A type test pattern consists of a predicate that specifies a type, along with a single binding variable.
instanceOf
operator matches obj
to the type test pattern.
If the test passes, casting is done automatically and variable s
is of type String
in the true
block of the if
statement.
In more complicated conditional expressions, the scope of s
grows as well.
if (obj instanceof String s && s.length() > 5) {
System.out.println(s);
}
Summary
Pattern matching for instanceOf
, along with other features introduced in Java 14,
make the language nicer to use and show that the decision to move to a 6 month release cadence back in 2017 helps the language to evolve faster.
Keep in mind, pattern matching for instanceOf
in Java 14 is a preview feature.
The implementation could change and evolve in upcoming releases.
To unlock preview features, use the --enable-preview
command line flag for javac
and java
.