Pattern Matching for Switch in Java 17, 18 and 19
Dewni.jpeg

Dewni De Silva

2022-09-06

devnis.svg

It seems like yesterday when programmers used to marvel at the changes brought by Java 8. Despite being 10 versions old at this point, as of 2022, 46% of all the softwares in production uses Java 8. Part of the reason was Oracle offering long term support but another part seems to be upgrading from Java 8 to a newer version was a huge task in itself. Then a blink and 8 years laters, Java 17 is here! Well if you think that’s exciting, blink again cause, so is Java 18! So what changed for programmers in Java 17? And where did Java 18 come from?

Java 17 was released in September 2021. This is also the version that Oracle was offering long term support after Java 11. The next to have this status is Java 21 set to release in 2023. This version introduced 14 features and enhancements. Java 18 on the other hand was released in March of this year, thanks to the new shorter release cycle of Java. Which means Java 19 will be here in September!

Undoubtedly the most buzz is around the feature “Pattern matching for switch”. Which comes to programmers in Java 17 and 18 (and 19) as a “preview feature” . What does that mean?

According to Oracle, a preview is a feature that is fully specified and implemented but has not been made permanent, intended as a way to gain developer feedback. A preview feature may or may not be released as is in the next twelve month. Not all upcoming features of Java will be released as preview features.

Switch statements have evolved so much from where it began. Java 7 allowed developers to use strings in switch, and hava 12 introduced switch “expressions”and arrow functions to return values from a switch expression (which means an expression which evaluates to a single statement). Java 17 only adds to that excitement.

For starters, once the feature is made permanent you will be able to check for null inside the switch statement! Traditionally, switch threw a null pointer exception if the value of the object passed was null. So previously you’d have to do this to be safe,

patternMatchingOne.png

Instead now you can do this, patternMatchingTwo.png

That is just so much less code! However if you don’t have “case null” in the switch expressions it will still throw a NullPointerException, because the default label != a null case (think of how many old codes will throw a complete fit, if that was the case!)

That is cool and all, but what is pattern matching in a switch?

Well for starters you can pass any object to switch statements and have cases check for individual types.

No more, patternMatchingThree.png

You can simply write the same as, patternMatchingFour.png (Don’t forget to add a case null -> or Java will add one for you and make it throw a NullPointerException)

With all this new excitement, it’s important to remember that switch blocks should be “complete”. Meaning that it needs to handle all possible values of the selector. This doesn’t mean that you have to check for all possible combinations of Objects in the above example, only that it will not work without the default statement, because there’s so much more to Java than Integers or Strings.

But wait, there’s more!

Suppose you match your object to a class but you still need to check a property of that class? Don’t worry! Oracle got you. Introducing guarded patterns!

patternMatchingFive.png

Right now Oracle states that patterns in switch do not support primitive types like int, boolean and double. But it still gives everyone plenty of toys to play with.

This feature is still in preview for Java 18 and 19! Giving developers plenty of time to enable preview and play around with it. Maybe when Java 20 rolls around in March 2023 it’ll be a permanent feature.

If you have been using Records introduced in Java 14 then Java 19 will preview another exciting feature for you. Instead of checking the Record type with instanceOf and using getters to get attributes of a records, Java will let you pattern match the entire Record and extract the attributes at the same time!

With the shorter release cycle of Java updates, developers get to enjoy newest features sooner than ever. So be sure to Google what’s new with Java, once in a while!

Share on: