Skip to content
RP

Rajko Pavlovic

1 article

July 22, 2022

Pattern Matching for instanceof and Switch in Java

Software Development

Pattern Matching for instanceof and Switch in Java

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. (more…)

Ready to Achieve More?

We’ll help you reach your goals quickly with an easy and straightforward process to kick off our collaboration. Here’s what happens next.

STEP 1

Discovery Call

Let’s chat to understand your company, project needs, and answer any questions along the way.

STEP 2

Free Consultation

Work closely with our experts to explore the right solutions for your business.

STEP 3

Collaboration Proposal

We'll recommend the best strategy for your goals, ensuring you get the most from our expertise.

STEP 4

30-Day Cancellation
Policy Contract

Spoiler: It’s Never Been Used

Enjoy peace of mind while we deliver excellence from day one—our track record speaks for itself.

Services you're interested in (Optional)