Java 12#
Switch Expressions (Preview) JEP 325#
More on this in Switch Expression
Java 13#
Switch Expressions (Second Preview) JEP 354#
More on this in Switch Expression
Java 14#
Switch Expression#
Below are the changes in the switch construct introduced in Java 14, formalized by JEP 361, JEP-354, and JEP-325. The new Switch Expression addresses three main issues with the traditional switch statement: verbosity, variable scope issues, and being limited to a statement.
1. Its unnecessarily verbose and error prone#
The traditional switch required break statements, leading to repetitive code and the potential for fall-through errors if a break was missed.
Problem Example (Traditional Switch Statement):
switch (day) { case MONDAY: case FRIDAY: case SUNDAY: System.out.println(6); break; case TUESDAY: System.out.println(7); break; case THURSDAY: case SATURDAY: System.out.println(8); break; case WEDNESDAY: System.out.println(9); break; }Solution (Switch Expression with
->):The new syntax uses
case L ->, which eliminates the need for an explicitbreakand allows multiple labels to be grouped.switch (weekDay) { case MONDAY, FRIDAY, SUNDAY -> System.out.println(6); case TUESDAY -> System.out.println(7); case THURSDAY, SATURDAY -> System.out.println(8); case WEDNESDAY -> System.out.println(9); }
2. Scope of variable within Switch#
A variable declared in one case block in the traditional switch is scoped to the entire switch block, causing errors if the same variable name is used in another arm.
Problem Example (Variable Scope):
switch (day) { case MONDAY: case TUESDAY: int temp = ...; break; case WEDNESDAY: case THURSDAY: int temp = ...; // Can't call this variable 'temp' break; default: int temp = ...; // Can't call this variable 'temp' }Error: Variable ’temp’ is already defined in the scope
Workaround: The common workaround was declaring a variable outside the
switchand assigning values inside eachcase.int temp; switch (day) { case MONDAY: case FRIDAY: case SUNDAY: temp = 6; break; case TUESDAY: temp = 7; break; case THURSDAY: case SATURDAY: temp = 8; break; case WEDNESDAY: temp = 9; break; default: throw new IllegalStateException("Wat: " + day); }Solution: Using
yieldor Expression Form: The new switch can return a value, naturally handling scope.- Using
yield(with:): Theyieldkeyword is used to produce a value from theswitchexpression.
Integer temp = switch (weekDay) { case MONDAY, FRIDAY, SUNDAY: yield 6; case TUESDAY: yield 7; case THURSDAY, SATURDAY: yield 8; case WEDNESDAY: yield 9; };- Using
->(as expression): This concise syntax implicitly yields the result.
Integer temp = switch (weekDay) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; };- Using
3. Limited to a statement not expression#
The traditional switch was limited to a statement and could not compute and return a single value directly.
As Statement (Original):
int numLetters; switch (day) { case MONDAY: case FRIDAY: case SUNDAY: numLetters = 6; System.out.println(6); break; case TUESDAY: numLetters = 7; System.out.println(6); break; case THURSDAY: case SATURDAY: numLetters = 8; System.out.println(6); break; case WEDNESDAY: numLetters = 9; System.out.println(6); break; default: throw new IllegalStateException("Wat: " + day); }As Expression (New Feature): The
switchcan now be used as an expression, where its result can be directly used in a statement likeSystem.out.println().System.out.println( switch (weekDay) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; } );
Java 15#
Java 16#
Java 17#
Some important links#
| JEP | Link |
|---|---|
| JEP-354 | https://openjdk.org/jeps/354 |
| JEP-325 | https://openjdk.org/jeps/325 |
| JEP 361 | https://openjdk.org/jeps/361 |
