JDK 14 – JEP 361 Switch Expressions out from preview
In my previous post, I wrote about switch expressions and related enhancements released as a preview feature for JDK 12. Subsequently, in JDK 13 there were some changes proposed, like using yield
keyword to return value from the switch block and released in preview.
In the upcoming JDK 14 release, which will go GA in March next year, these changes in switch
will be out from preview and made final and permanent. In this post, we will look at what has changed over the two releases and re-run the examples I shared in my previous post on JDK 14.
Switch as an expression
In the below code snippet we will see how to use switch
as an expression i.e evaluate some conditions and return a value:
01 02 03 04 05 06 07 08 09 10 11 | public static boolean isHealthy(String food){ return switch (food){ case "Fruit" -> true ; case "Vegetable" -> true ; case "Pizza" -> false ; case "Burger" -> false ; case "Pulses" -> true ; case "Soft Drink" -> false ; default -> false ; }; } |
We can use string literals as case expressions while this was not possible prior to JDK 12. The above can be written using Enums in which case we wouldn’t need the default
block:
01 02 03 04 05 06 07 08 09 10 | public static Boolean isHealthEnumVersion(Food food){ return switch (food){ case Fruit -> true ; case Vegetable -> true ; case Pizza -> false ; case Burger -> false ; case Pulses -> true ; case Soft_Drink -> false ; }; } |
The Food
enum is defined as:
1 2 3 | enum Food { Fruit, Vegetable, Pizza, Burger, Pulses, Soft_Drink } |
Switch expression with a block of code for a case evaluation
In the previous example, we saw that the case
handled only a single line expression. What if we wanted to execute multiple statements and then return a result? This can be achieved using theyield
keyword.
In JDK 12, the break
keyword was overloaded to return the value as well. But this wasn’t appreciated by all and hence a new keyword yield
was added for returning the value.
The below code snippet executes a block of code and returns a value:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | public static PreparedFood prepareFood(Food food){ return switch (food){ case Pizza -> { System.out.println( "doing pizza related operations" ); yield new PreparedFood(food); } case Burger -> { System.out.println( "Doing burger related operations " ); yield new PreparedFood(food); } default -> { System.out. printf ( "Doing %s related operations\n" , food.toString()); yield new PreparedFood(food); } }; } |
The yield
can also be used in the old switch syntax as shown below:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | public static PreparedFood prepareFoodOldSyntax(Food food){ return switch (food){ case Pizza: System.out.println( "doing pizza related operations" ); yield new PreparedFood(food); case Burger: System.out.println( "Doing burger related operations " ); yield new PreparedFood(food); default : System.out. printf ( "Doing %s related operations\n" , food.toString()); yield new PreparedFood(food); }; } |
The source code for this can be found in the repository here.
Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: JDK 14 – JEP 361 Switch Expressions out from preview Opinions expressed by Java Code Geeks contributors are their own. |
Regarding the statement “We can use string literals as case expressions while this was not possible prior to JDK 12”. As such this is possible (strings in switch) from Java-7 and later itself.
Hi…
I’m Elena gillbert.Extend switch so it can be used as either a statement or an expression, and so that both forms can use either traditional case … : labels (with fall through) or new case … -> labels (with no fall through), with a further new statement for yielding a value from a switch expression. These changes will simplify everyday coding, and prepare the way for the use of pattern matching in switch. This was a preview language feature in JDK 12 and JDK 13.