Home » Java » Core Java » Java 17: Pattern Matching for Switch

About Fahd Shariff

Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.

Java 17: Pattern Matching for Switch

In Java 17 (released only a few days ago), Pattern Matching for switch has been introduced as a preview language feature, which allows case labels with patterns rather than just constants. Here is an example showing how you can match on type patterns:

public static String typedPatternMatching(Object o) {
  return switch(o) {
    case null      -> "I am null";
    case String s  -> "I am a String. My value is " + s;
    case Integer i -> "I am an int. My value is " + i;
    default        -> "I am of an unknown type. My value is " + o.toString();
  };
}

// Output:
> typedPatternMatching("HELLO")
"I am a String. My value is HELLO"

> typedPatternMatching(123)
"I am an int. My value is 123"

> typedPatternMatching(null)
"I am null"

> typedPatternMatching(0.5)
"I am of an unknown type. My value is 0.5"

You can also use a guarded pattern in order to refine a pattern so that it is only matched on certain conditions, for example:

public static String guardedPattern(Collection<String> coll) {
  return switch(coll) {
    case List list && (list.size() > 10) -> 
        "I am a big List. My size is " + list.size();
    case List list -> 
        "I am a small List. My size is " + list.size();
    default -> 
        "Unsupported collection: " + coll.getClass();
  };
}

If you have a Sealed Class (made a permanent language feature in Java 17), the compiler can verify if the switch statement is complete so no default label is needed. For example:

sealed interface Vehicle permits Car, Truck, Motorcycle {}
final class Car implements Vehicle {}
final class Truck implements Vehicle {}
final class Motorcycle implements Vehicle {}

public static String sealedClass(Vehicle v) {
  return switch(v) {
    case Car c -> "I am a car";
    case Truck t -> "I am a truck";
    case Motorcycle m -> "I am a motorcycle";
  };
}

Published on Java Code Geeks with permission by Fahd Shariff, partner at our JCG program. See the original article here: Java 17: Pattern Matching for Switch

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments