Switch Improvements
How many of us remember the use of switch case in our daily programming world follows closely languages such as C and C++, and supports fall-through semantics by default. With this traditional control flow is often useful for writing low-level code (such as parser for binary encoding), as switch is used in higher-level contexts, its error-prone nature starts to outweigh its flexibility.
So before we jump on the improved version of switch let’s have a look at what the previous version taught us. So here we have taken an example of an integer which is being passed into the method and that integer which is 3 is passed inside the switch statement . Here’s one way to do that:
As you may have seen that the other switch occurrences out in the wild, this one just wants to compute a value and assign it, but the implementation is roundabout (declare result to use it later), it is repetitive, and error-prone.
This also can be verified with the test case as below which is better practice now days for the correct result prediction.
Now, In the new introduced switch statement colon (:) after each case statement has been replaced with a -> operator. This operator is used inside of switch expressions to signal to the Java compiler that this case statement selects a return value for the switch expression, rather than selecting a block of code to execute.
To elaborate more on the new improved switch let’s have a look at the below code:
With the above implementation two things are being highlighted
1. switch can have a result
2. what’s this lambda doing here
The case statements have no break keyword after them. Since the value selected with the -> operator is interpreted as the return value of the switch expression, the break after each statement is implicit (unnecessary actually). You can also think of the -> as a return statement which returns a value from the switch expression, and thus breaks the execution of the switch expression.
We can use the new “arrow” syntax(“switch labelled rules”) i.e Lambda Expression with the enhanced switch statement.
So, before Java 12 switch was a statement an imperative construct that directs control flow. It shows the way, but — so to speak — can never be the destination. Because the ultimate goal of any computation is a result, a value. An expression, on the other hand, gets evaluated to exactly that: a value.
Now this leads to the difference between Java if and the conditional operator ? :. Both check a Boolean condition and branch execution according to that. The difference is that if merely executes the respective block, whereas ? : is evaluated to the respective result.Same is the case for switch: Before Java 12, if we would have to compute a value, you had to either assign the result to a variable (and then break) or return from a method dedicated to the switch statement. Now, the entire switch statement expression is evaluated.
Now the above code demonstrates the use of a switch which looks very similar to the switch expression that we have been using since ages. However, there is a significant difference between the old switch and the new switch, one difference is that this switch expression returns a result that is assigned to the local variable “numericString”.
The second difference, which directly relates to the switch expression being able to return a value, is that the break clauses now each have the value to be returned for the relevant case immediately specified after the break keyword. In essence, the break in the switch expression acts like at the Java method return.
Now as we have seen above there are more advantages of using a new switch case one can specify multiple statements to occur for a single case before returning a single value. Let’s understand this with an example:
In most cases, however, it will likely become popular to return a value from a switch expression using the arrow syntax as discussed earlier to benefit from no risk of fall-through and to avoid scope surprises commonly associated with the normal switch statement.
One consequence of the distinction between expression and statement is that a switch expression, since it’s part of a statement, needs to end with a semicolon, whereas the classic switch statement doesn’t.
Now, till here it is clear that we have used the new lambda-style syntax with the arrow between label and execution. It is important to understand that this is not required to use switch as an expression. Then, there is a new keyword yield. It is similar to the return statement. But for switch expression instead. It marks the value to be returned from a switch branch. Because yield terminates the switch expression, you don’t need to have a break after it.
Now, it may come to our mind if we can mix the traditional switch expression with the newly introduced switch, however the JDK 12 compiler doesn’t allow you to mix the “arrow” syntax and traditional colon switch statement. Attempting to do so will lead to an error message “error: different kinds used in the switch”.