In Java 12, switch-case statement has been improved as part of ‘Java Enhancement Proposal’ JEP 305 (JDK-8222184). Lets look at examples of these improvements.
Setup for preview feature
Switch enhancements in JDK 12 are preview only features i.e. these enhancements are not enabled by default. If you directly write code to use this, it will give compiler error Switch Expressions is a preview feature and disabled by default. Use --enable-preview to enable
If you are using eclipse IDE, then right click on project > Properties > Java Compiler > Uncheck “Use default compliance settings” > Check “Enable preview feature for Java 12”
After enabling preview feature, compiler will continue to give warning
You are using a preview language feature that may or may not be supported in a future release wherever preview features are used.
Arrow Labels without fall through
- In addition to case X: (colon), new syntax introduced i.e. case X -> (arrow)
- case X: (colon) will fall-through i.e. if no break; found, then it will go to next case Y:. case X -> (arrow) will not fall-through i.e. it will only execute code of right on arrow & will end switch. It won’t go to next case so no need of break.
- If case (arrow) has multiple lines of code, then use code block as shown in case ‘0’ in below example. Otherwise JDK will give compiler error Syntax error on token ";", SwitchLabels expected after this token
- Case styles i.e. “:” & “->” can not be combined in single switch as JDK will give compiler error Mixing of different kinds of case statements '->' and ':' is not allowed within a switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.itsallbinary.java.java12; public class Java12Switch_ColonVsArrow { public static void main(String[] args) { int i = 2; // Case with colon. Needs break; switch (i) { case 0: System.out.println("a) With colon - Zero "); System.out.println("b) With colon - Zero"); break; case 1: System.out.println("With colon - One"); break; case 2: System.out.println("With colon - Two"); break; case 3: System.out.println("With colon - Three"); break; } // Case with arrow. Doesn't need break; switch (i) { // Multiple lines of code put in code block case 0 -> { System.out.println("a) With arrow - Zero "); System.out.println("b) With arrow - Zero"); } case 1 -> System.out.println("With arrow - One"); case 2 -> System.out.println("With arrow - Two"); case 3 -> System.out.println("With arrow - Three"); } // Case with arrow & multiple values. switch (i) { case 0 -> { System.out.println("a) With arrow - Zero "); System.out.println("b) With arrow - Zero"); } // Multiple cases with comma separated case 1, 2, 3 -> System.out.println("With arrow (Multiple) - One, Two, Three"); } } } |
1 2 3 |
With colon - Two With arrow - Two With arrow (Multiple) - One, Two, Three |
Switch Expression
Switch expression allows switch to be used directly to assign value using assignment operator or also in method arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.itsallbinary.java.java12; public class Java12_Switch_Expressions { public static void main(String[] args) { int i = 2; // Switch expression with assignment operator String number = switch (i) { case 0 -> "With arrow - Zero "; case 1 -> "With arrow - One"; case 2 -> "With arrow - Two"; case 3 -> "With arrow - Three"; default -> "NA"; }; System.out.println("(Assignment operator) Number is " + number); // Switch expression as method argument or parameter System.out.println("(Method argument) Number is " + switch (i) { case 0 -> "With arrow - Zero "; case 1 -> "With arrow - One"; case 2 -> "With arrow - Two"; case 3 -> "With arrow - Three"; default -> throw new IllegalArgumentException("Unexpected value: " + i); } ); } } |
1 2 |
(Assignment operator) Number is With arrow - Two (Method argument) Number is With arrow - Two |
Switch expression must cover all possible values of variable used in switch like ‘enum’. Not doing so will cause a compilation error as shown below.
1 2 3 4 5 6 7 |
DayOfWeek dayOfWeek = DayOfWeek.TUESDAY; int i = switch (dayOfWeek) { case SUNDAY -> 1; case MONDAY -> 2; case TUESDAY -> 3; }; System.out.println("Switch result for " + dayOfWeek + " is " + i); |
1 |
A Switch expression should cover all possible values |
Yield keyword
If multiple lines of code is required with switch expression, yield keyword can be used to provide final value to assignment. Yield can also be used with regular switch case (colon) to use it as switch expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.itsallbinary.java.java12; public class Java12_Switch_YieldKeyword { public static void main(String[] args) { int i = 2; // Switch expression with yield String numberYield = switch (i) { case 0 -> "With arrow - Zero "; case 1 -> "With arrow - One"; case 2 ->{ String twoResult = "(Yield) With arrow"; twoResult = twoResult + " - Two"; yield twoResult; } case 3 -> "With arrow - Three"; default -> "NA"; }; System.out.println("(Switch expression yield) Number is " + numberYield); // Regular Switch case with ":" with yield String numberYieldColon = switch (i) { case 0: yield "With colon - Zero "; case 1: yield "With colon - One"; case 2: { String twoResult = "(Yield) With colon"; twoResult = twoResult + " - Two"; yield twoResult; } case 3: yield "With colon - Three"; default: yield "NA"; }; System.out.println("(Regular switch case with yield) Number is " + numberYieldColon); } } |
1 2 |
(Switch expression yield) Number is (Yield) With arrow - Two (Regular switch case with yield) Number is (Yield) With colon - Two |