Pattern Matcing For Switch
Pattern Matcing For Switch
Pattern Matching for switch is a feature introduced in Java 17 (as a preview) and enhanced in later
versions, including Java 21. It allows for more flexible and expressive control flow by integrating
pattern matching directly into the switch statement. This feature simplifies handling different types
or patterns within a single switch construct, making the code more concise and readable.
1. Pattern Matching: This enables you to specify the shape or type of an object and perform
operations based on that shape.
2. Type Patterns: In switch statements, you can match the type of an object and bind it to a
variable in one step.
3. Exhaustive Checking: The compiler ensures all possible cases are covered, leading to safer
code.
4. Null Safety: switch can handle null values explicitly, avoiding potential NullPointerExceptions.
1. Simplifies Control Flow: Reduces the need for manual instanceof checks and type casting,
streamlining code that needs to handle multiple types or patterns.
2. Cleaner Code: Combining type checks and casts into a single operation makes the code more
concise.
3. Better Readability: Pattern matching makes complex branching logic easier to follow by
keeping everything within the switch statement.
String day="Monday";
switch(day){
case "Saturday","Sunday":
System.out.println("6am");
break;
case "Monday":
System.out.println("8am");
break;
default:
System.out.println("7am");
}
}
}
now we can implement the Switch case like this: (we are using -> so we do not have to use the break
statement.)
String day="Monday";
switch(day){
case "Saturday","Sunday"-> System.out.println("6am");
default-> System.out.println("7am");
}
}
}
We can also use a variable here like this i.e (result ):
String day="Monday";
String result = "";
switch(day){
case "Saturday","Sunday" -> result="6am";
}
System.out.println(result);
}
}
We can also remove the result from multiple line like this:
String day="Monday";
String result = "";
result = switch(day)
{
case "Saturday","Sunday" -> "6am";
};
System.out.println(result);
}
}
If we want to used the yield instead of (arrow ->) that we can do like this :
String day="Monday";
String result = "";
result = switch(day)
{
case "Saturday","Sunday" : yield "6am";
default : yield"7am";
};
System.out.println(result);
}
}
Another Example:
Let’s take a look at how pattern matching simplifies switch statements with an example.
Explanation:
Type Matching: In the switch, we check for types like Integer, String, and Double. If the
object matches a type, it's automatically cast, and we can use the variable (i, s, or d) in that
block.
Null Handling: A specific case for null can be added, ensuring null-safe operations.
Cleaner Code: We no longer need instanceof checks and explicit casting; the pattern
matching handles both.
Example 2: Enhanced Matching with Complex Types
printDetailedInfo(obj);
switch (obj) {
case Double d when d > 40.0 -> System.out.println("Large double: " + d);
Explanation:
Guarded Patterns (when clause): We can add conditional logic to refine the pattern. For
instance, case Integer i when i > 50 matches only integers greater than 50.
Multiple Pattern Matches: The switch can handle both types and conditions within the same
construct, allowing fine-tuned control flow.