In Java 12 and Java 13, Switch Expressions are added as a preview feature, and in Java 14 became a standard feature.
The Switch Expressions feature extends the regular Java switch statements and it can be used as either a statement or an expression. The entire switch block gets a value that can then be assigned to a variable.
Examples of switch Statement in Java
Regular Java switch statement:
class Test { public static void main(String[] args) { String color = "blue"; switch (color) { case "white": System.out.println("The color is white."); break; case "yellow": System.out.println("The color is yellow."); break; case "blue": System.out.println("The color is blue."); break; case "green": System.out.println("The color is green."); break; default: System.out.println("Unrecognized color!"); } } }
Output: The color is blue.
Now, this can be written like this:
class Test { public static void main(String[] args) { String color = "blue"; switch (color) { case "white" -> System.out.println("The color is white."); case "yellow" -> System.out.println("The color is yellow."); case "blue" -> System.out.println("The color is blue."); case "green" -> System.out.println("The color is green."); default -> System.out.println("Unrecognized color!"); } } }
Output: The color is blue.
As you can see, the new -> arrow sign got introduced. And we don’t need to use break anymore.
Switch Expressions with multiple statements:
class Test { public static void main(String[] args) { String day = "Tuesday"; switch (day) { case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> System.out.println("It is a week day."); case "Saturday", "Sunday" -> System.out.println("It is a weekend."); } } }
Output: It is a week day.
We can also assign a switch expression to a variable:
class Test { public static void main(String[] args) { String day = "Tuesday"; String result = switch (day) { case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Weekday"; case "Saturday", "Sunday" -> "Weekend"; default -> throw new IllegalStateException("Unexpected value: " + day); }; System.out.println(result); } }
Output: Weekday
The value after the -> sign will be returned. We can also avoid using the -> sign by replacing it with : , just in this case, if we want to return some value, we need to use the newly introduced keyword yield.
yield Keyword Example in Java
class Test { public static void main(String[] args) { System.out.println(isNumberLessThenFive(4)); System.out.println(isNumberLessThenFive(8)); } static boolean isNumberLessThenFive(int number) { return switch (number) { case 1, 2, 3, 4: yield true; case 5, 6, 7, 8, 9, 10: yield false; default: throw new IllegalStateException("Unexpected value: " + number); }; } }
Output: true false
Some key points:
- We don’t need to put break statement anymore
- If we use -> (arrow) sign, we can skip the yield keyword
- If we use : instead of -> we need to use the yield keyword if the switch case needs to return some value
- If we have multiple statements inside one switch case block, we need to put them inside the {} curly braces.
That’s it!