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