Pattern Matching for Switch (introduced in Java 14)

 Pattern Matching for Switch (introduced in Java 14)


Pattern matching for switch simplifies the common use case of performing different actions based on the value of a variable. It eliminates the need for repetitive casting and enhances code readability. Here's an example:


String day = "Sunday";


switch (day) {

    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ->

        System.out.println("It's a weekday.");

    case "Saturday", "Sunday" ->

        System.out.println("It's a weekend day.");

    default ->

        System.out.println("Invalid day.");

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...