The New Switch Case Features
0 CommentsLast Updated on February 3, 2021 by jt
Java SE 12 introduces switch expressions
, which like all expressions evaluate to a single value, that you can use as statements. Java 12, brings improvement to the switch statement with new capabilities.
Some of the key capabilities are arrow case labels, multiple case statements, and return values.
In this post, I’ll explain the new switch case features.
Using Arrow Case Label “->”
To break out of different cases we generally use break
statements. Now with the enhancement, you can use the new arrow case label
. This allows the expression on the right to be executed if the label matches. As a result, It eliminates the need of using multiple break
statements in the case label statements.
This code shows using arrow case labels.
UsingSwitchCase.java
package com.springframeworkexpression; public class UsingSwitchCase { public static void display(int k) { switch (k) { case 1 -> System.out.println("One"); case 2 -> System.out.println("Two"); case 3 -> System.out.println("Three"); case 4 -> System.out.println("Four"); case 5 -> System.out.println("Five"); default -> System.out.println("Wrong input provided"); } } }
This preceding code uses arrow case labels (-> )
instead of traditional colon case labels(:)
. This prevents fall through.
The test code is:
//For arrow case UsingSwitchCase usingSwitchCase = new UsingSwitchCase(); usingSwitchCase.display(3);
The output is this:
Multiple Comma-separated Labels in a Single Switch Case
Rather than forcing the fall-through semantics of switch statements, you can list multiple comma-separated case labels on the same line. Doing so makes code both easier to read and understand.
MultipleCases.java
package com.springframeworkexpression; public class MultipleCases { public static void display2(int k2) { switch (k2){ case 1,2,3,4,5 -> System.out.println("Entered value is between 1 to 5 "); case 6,7,8,9,10 -> System.out.println("Entered value is between 6 to 10"); default -> System.out.println("Enter value between 1 to 10"); } } }
The test code is:
//For mutliple cases MultipleCases multipleCases = new MultipleCases(); multipleCases.display2(6);
The output is:
Returning Values from the Switch Statement
Switch cases can return specific values, depending upon the input provided, To simplify this switch statements
can now return values. which removes the additional need to create a variable specifically for the purpose of returning a set value. You can return values either by using break
or by arrow rules.
ValueReturn.java
package com.springframeworkexpression; public class ValueReturn { public static String printValueOfQuarter(String nameOfMonth){ String quarter = null; switch (nameOfMonth){ case "January", "February" , "March" ->quarter ="Quarter1"; case "April","May","June" ->quarter = "Quarter 2"; case "July", "August","September" ->quarter ="Quarter 3"; case "October","November","December" -> quarter ="Quarter 4"; default -> System.out.println("Wrong input provided"); } return quarter; } }
The test code is:
//For return value ValueReturn valueReturn = new ValueReturn(); String result2 = valueReturn.printValueOfQuarter("June"); System.out.println("For value return class: "+result2);
The output is:
Block Scope and Yield
The new yield
statement. takes one argument which is the value that the case label produces in a switch
expression. The yield
statement makes it easier to differentiate between switch statements
and switch expressions
.
Due to fall through semantics, the scope of switch statements
applies to the entire statement. This means that two separate cases could not use the same variable names. Now with the new changes, the scoping can be done at a case level. This allows for cleaner and flexible code.
Main.java
int i; String month = blockScope.getMonth(); String quartName = switch (month){ case "January","February","March" -> { i=1; System.out.println("Block 1"); yield "Quarter 1"; } case "April","May","June" -> { i=2; System.out.println("The value of i is " +i); System.out.println("Block 2"); yield "Quarter 2"; } case "July" ,"August","September " ->{ i=3; System.out.println("The value of i is " +i); System.out.println("Block 3"); yield "Quarter 3"; } case "October","November","December" ->{ i=4; System.out.println("The value of i is " +i); System.out.println("Block 4"); yield "Quarter 4"; } default -> "wrong input"; }; System.out.println("Quarter name is :" +quartName); } }
The test code is:
//For yield and blockspace BlockScope blockScope = new BlockScope(); String result = blockScope.printValueOfQuarter("March"); System.out.println("For block scope class "+result);
The output is: