Pattern matching in switch statement in Java 17+

problem

You are learning the new features introduced to Java version 17 and above. In this post we will see how to use pattern matching in a switch statement. Pattern matching existed in other languages like Scala, Haskel. Java adopted this feature as well.

SOLUTION

Let’s say we have an Object and wish to perform different actions according to its data type.

PatternMatching.java
				
					Object test = true;

switch (test) {
    case null -> System.out.println("It's null");
    case Integer i -> System.out.println("It's Integer");
    case String s -> System.out.println("It's a String");
    case Character c -> System.out.println("It's a character");
    case Boolean b -> System.out.println("It's a boolean");
    default -> System.out.println("It's default");
}

				
			

The syntax of switch statement accepts a parameter that in this case is the test object that we want to perform checks on it. 

Because we have few checks to perform, a pattern matching comes very handy in contrast with an if – else statement.

Let’s put it in a main method to run it.

PatternMatching.java
				
					public class PatternMatching {
	
	public static void main(String ...args) {
		System.out.println("Pattern Matching program\n");

		Object test = true;

		switch (test) {
			case null -> System.out.println("It's null");
			case Integer i -> System.out.println("It's Integer");
			case String s -> System.out.println("It's a String");
			case Character c -> System.out.println("It's a character");
			case Boolean b -> System.out.println("It's a boolean");
			default -> System.out.println("It's default");
		}
		System.out.println();
	}
}
				
			
running

In the terminal:

PatternMatching.java
				
					javac PatternMatching.java && java PatternMatching
				
			
output
Pattern matching in java example boolean

Let’s change the value of test variable to null.

PatternMatching.java
				
					public class PatternMatching {
	
	public static void main(String ...args) {
		System.out.println("Pattern Matching program\n");

        Object test = null;

		switch (test) {
			case null -> System.out.println("It's null");
			case Integer i -> System.out.println("It's Integer");
			case String s -> System.out.println("It's a String");
			case Character c -> System.out.println("It's a character");
			case Boolean b -> System.out.println("It's a boolean");
			default -> System.out.println("It's default");
		}
		System.out.println();
	}
}
				
			
output
Pattern matching in java example null

Let’s change the test variable now to a string.

PatternMatching.java
				
					public class PatternMatching {
	
	public static void main(String ...args) {
		System.out.println("Pattern Matching program\n");

        Object test = "pattern matching is cool";

		switch (test) {
			case null -> System.out.println("It's null");
			case Integer i -> System.out.println("It's Integer");
			case String s -> System.out.println("It's a String");
			case Character c -> System.out.println("It's a character");
			case Boolean b -> System.out.println("It's a boolean");
			default -> System.out.println("It's default");
		}
		System.out.println();
	}
}
				
			
output
Pattern matching in java example string

And so on.

conclusion

In this post we saw a basic example of how to use pattern matching in a switch statement in Java version 17 and later.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.