Using new Switch expressions
One of the new features released as part of Java 14 is the switch expressions. This feature was available in earlier versions (Java 12 and 13), too, but as a preview. In Java 14, it became standard. The main difference between the existing and new switch is, the new switch is not the only statement but also expression. There are some more differences, which we will learn in this article.
- Syntax of the new switch expression
- How to use the new switch expression?
Syntax of the new switch expression
On the right hand side of ‘case L1 ->’ we can have an expression, a block or a throw statement.
How to use the new switch expression?
The Existing switch has some problems and which are the motive for adding new switch expression. Below is the list of features of the new switch expression.
Statement & Expressions
First and foremost, as I mentioned earlier is, the existing switch is a statement, whereas a new switch can be used as a statement and also as an expression. Expressions can reduce a lot of boilerplate; they can be used along with the return or passed as an argument.
Switch expression is passed as an argument to System.out.println(). It is not possible with the existing switch statement.
yield is the new keyword introduced along with switch expression. We may have a block on the right side of ‘case L ->,’ if so, then we need to use yield to return value from the block, which becomes the value of the enclosed switch statement. For a single expression, we don’t need to have a yield.
Arrows and Semicolon
Switch case statements can have arrows (‘case L1 ->’) or semicolons (‘case L2:’). The main difference between these is, with an arrow, an expression is enough to return a value. Whereas with a semicolon, use ‘yield’ to return value. yield keyword will help us to identify that we are using new switch expression, not the existing switch statement.
The whole switch block is treated as a single scope in the current switch statement, whereas the scope for each case arm is different in the new switch.
Variable ‘result’ is used in multiple cases because the scope is different for each case, whereas in the existing switch, the whole switch will have the same scope.
New switch expression can have multiple constants per case.
New switch expression fixes many irregularities of the existing switch statement and try to use it to express multi-way conditionals as expressions.