# Pattern Matching for Switch in Java 17+ (with Examples)

Switch statements have been a staple in Java for decades, but prior to Java 17, they were **limited** — you could only switch on constants and enums.

With **Java 17+**, **pattern matching for switch** allows you to match types directly, making your code **cleaner, safer, and more expressive**.

## What Is Pattern Matching for Switch?

Pattern matching for switch allows you to:

* Test the type of a value directly in the `case` statement.
    
* Automatically cast the value to the matched type.
    
* Combine multiple patterns in one switch.
    

This reduces boilerplate and improves readability.

Basic Example Before Java 17

```java
Object obj = "Hello";

if (obj instanceof String) {
    String s = (String) obj;
    System.out.println("String value: " + s);
} else if (obj instanceof Integer) {
    Integer i = (Integer) obj;
    System.out.println("Integer value: " + i);
}
```

Notice the **manual casting** in every `instanceof` block — verbose and repetitive.

## Pattern Matching for Switch in Java 17+

Java 17+ allows you to combine **switch** with **type patterns**:

```java
public class SwitchPatternExample {
    public static void main(String[] args) {
        Object obj = 123;

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

Output:

```java
Integer: 123
```

Notice:

* The variable (`s`, `i`, `d`) is **automatically typed**.
    
* No need for manual casting.
    

## Using Multiple Patterns in One Case

You can match **multiple types** in a single case:

```java
Object obj = 3.14;

switch (obj) {
    case String s, Integer i -> System.out.println("String or Integer: " + obj);
    case Double d -> System.out.println("Double: " + d);
    default -> System.out.println("Unknown type");
}
```

Output:

```java
Double: 3.14
```

## Combining Guard Conditions (When Clauses)

Java 17+ allows **guarded patterns** to refine matching:

```java
Object obj = 42;

switch (obj) {
    case Integer i && i > 0 -> System.out.println("Positive Integer: " + i);
    case Integer i -> System.out.println("Non-positive Integer: " + i);
    case String s -> System.out.println("String: " + s);
    default -> System.out.println("Other type");
}
```

Output:

```java
Positive Integer: 42
```

## Benefits of Pattern Matching for Switch

* **Cleaner code**: no more repeated `instanceof` checks and casting.
    
* **Safer**: compiler ensures type safety automatically.
    
* **Readable**: easier to see all type-specific logic in one switch.
    
* **Flexible**: works with multiple patterns and guard conditions.
    

## Conclusion

Pattern Matching for Switch in Java 17+ is a **modern, elegant, and powerful** way to handle type-based logic.

It reduces boilerplate, improves readability, and ensures type safety — making your switch statements **more expressive and maintainable**.

If you’re using Java 17 or above, this is the **preferred way** to handle conditional logic based on types.
