Since Java 10, there has been a release cycle with two new releases each year, one in March and another in September. Each new release brings some new features. In 2021 there were two related features, pattern matching for instanceof
, and for switch statements.
Pattern matching was a preview feature since Java 14 (March 2020), and it is finalized for Java 16 (March 2021). The second feature expands pattern matching for instanceof
to support switch statements. It has been available for a preview since Java 17 (September 2021).
Problem
A common code pattern in Java programs was to use casting after checking if an object has a specific type.
if (o instanceof Integer) { Integer i = (Integer) o; //do something integer specific } else if (o instanceof String) { String s = (String) o; //do something string specific }
While not a significant issue, this definitely is not the most convenient solution. The problem grows more extensive with each new type introduced. This tedious repetition of the same code increases the chance for error and is not the most readable code.
Current solution
The first introduced solution for this problem is pattern matching for instanceof
operator. Now the instanceof
operator checks if an object matches some specified type pattern, and if the check passes, it will be cast and assigned to our new variable.
if (o instanceof Integer i) { i++; } else if (o instanceof String s) { s.contains(“a”); }
Another interesting use case this feature brings is using cast variable inside if statement on the right side of &&
operator.
If (o instanceof Integer i && i < 100 ) { //some code }
From the examples, it is clear that the cast variable is in scope on the right-hand side of the &&
operator and inside a true block.
Future solution
Since Java 17, there is a new feature for preview, support for type pattern matching in switch statements. It is a further improvement on writing if else chains for type checking. Now you can try writing type checking like this, and once the feature is finalized, you can start using it in your long-term projects. As of Java 18 (March 2022), this feature is in the second round of previews.
switch (o) { case null -> { //handle null case } case Integer i -> { //do something Integer specific with i } case String s -> { //do something String specific with s } default -> { //handle everything else } }
As you can see in the above example, there is a case null label. Alongside pattern matching for switch statements, there is support for null values in a switch. Previously if you passed a null to a switch Null Pointer Exception
would rise. That behavior is kept to ensure backward compatibility unless there is an explicit null case label.
Pattern matching for instanceof is a nice to have feature, but pattern matching inside switch statements is a real improvement, and hopefully, it will be finalized soon.
“Pattern matching for instanceof and switch in Java” Tech Bite was brought to you by Rajko Pavlović, Junior Software Engineer at Atlantbh.
Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.