How do you use pattern matching for switch with a simple code example?

Pattern matching for switch in Java allows developers to use more flexible and expressive switch cases compared to traditional switch statements. This feature improves code readability and maintainability.

keywords: java, switch, pattern matching, code example
description: This example demonstrates the use of pattern matching in switch statements in Java to simplify conditional logic.
public class SwitchPatternExample { public static void main(String[] args) { Object obj = "Hello"; switch (obj) { case Integer i -> System.out.println("Integer: " + i); case String s -> System.out.println("String: " + s); case null -> System.out.println("Object is null"); default -> System.out.println("Unknown type"); } } }

keywords: java switch pattern matching code example