Pattern matching for switch (further enhancements) in Java 22

Java 22 introduces further enhancements to pattern matching for switch statements. Pattern matching was first introduced in Java 14, enabling developers to use powerful conditional statements with concise and readable syntax. With the new enhancements in Java 22, pattern matching becomes even more flexible and convenient.

Table of Contents

Enhanced Syntax

Java 22 enhances the syntax of pattern matching for switch statements by introducing a more compact and expressive syntax. Now, you can use the case keyword followed by a pattern instead of a value.

switch (obj) {
    case Integer i -> System.out.println("An integer: " + i);
    case String s -> System.out.println("A string: " + s);
    case Double d -> System.out.println("A double: " + d);
    default -> System.out.println("Unknown type");
}

This new syntax makes the code more concise and eliminates the need for explicit type casting.

Null Checks

Java 22 introduces an enhanced null check pattern for switch statements. Now, you can use case null to handle null values directly within the switch statement.

switch (obj) {
    case null -> System.out.println("Object is null");
    case String s -> System.out.println("A string: " + s);
    default -> System.out.println("Unknown type");
}

This allows you to handle null values without having to write additional if conditions in your code.

Type Patterns

In Java 22, pattern matching for switch statements goes beyond simple equality checks. You can now use type patterns to match objects of a specific type or its subtypes.

class Vehicle {
    // ...
}

class Car extends Vehicle {
    // ...
}

class Bike extends Vehicle {
    // ...
}

Vehicle vehicle = new Car();

switch (vehicle) {
    case Car c -> System.out.println("Car: " + c);
    case Bike b -> System.out.println("Bike: " + b);
    case Vehicle v -> System.out.println("Vehicle: " + v);
}

This allows you to handle different subclasses of a superclass without explicit casting or instanceof checks.

Records and Patterns

Java 22 also introduces improved support for records and patterns. Pattern matching can now be used with records to perform matching based on record components.

record Point(int x, int y) {
    // ...
}

Point point = new Point(5, 10);

switch (point) {
    case Point(x: 0, y: 0) -> System.out.println("Origin");
    case Point(x: x, y: y) -> System.out.println("Coordinates: " + x + ", " + y);
    default -> System.out.println("Unknown point");
}

This allows you to destructure record components and perform matching based on their values.

Use Cases

Pattern matching for switch statements in Java 22 can be useful in various scenarios, including:

Conclusion

Java 22 introduces further enhancements to pattern matching for switch statements, making it easier and more flexible to handle different patterns in your code. The enhanced syntax, support for null checks, type patterns, and integration with records offer new possibilities for creating concise and readable code. These improvements in pattern matching contribute to the ongoing evolution of the Java language, providing developers with powerful tools to express their intent effectively.

Tags: #Java #PatternMatching