JDK 17 – Switch Case Performance

Switch statements in Java have evolved over time. Switch statements supported primitive types at the beginning, then came along String,  now JDK17 stable release is out and we have pattern matching which allows us to pass different object types to the switch statement.

We can now have switch statements like this: (example taken from Oracle JavaSE 17 docs)

How to Use Pattern Matching for instanceof From JDK16

The new LTS version (version 17) of Java was released in September. And since the previous LTS (version 11), many cool features were added. One of the coolest ones is “Pattern Matching for instanceof“.

Until Java 15, when we checked whether an object is an instance of a particular class, the compiler didn’t infer the variable’s type, even if the object passed the condition. Because of that, we always had to cast after the instanceof check.