Enhancements for Switch Statement in Java 13 - GeeksforGeeks

24 Oct, 2020
Java 12 improved the traditional switch statement and made it more useful. Java 13 further introduced new features. Before going into the details of new features, let’s have a look at the drawbacks faced by the traditional Switch statement.
1. Default fall through due to missing break
The default fall-through behavior is error-prone. Let’s understand it with an example.
switch (itemCode) {
    case 001 : 
        System.out.println("It's a laptop!");
        break;
    case 002 :
        System.out.println("It's a desktop!");
        break;
    case 003 :
        System.out.println("It's a mobile phone!");
        break;
    default :
        System.out.println("Unknown device!");
}

The above code works by matching the corresponding case and executing the particular code block. As long as you provide the necessary break statements, it works fine.
But what happens if we forget any of the required break statements:
 switch (itemCode) {
    case 001 : 
        System.out.println("It's a laptop!");
        // missed out break here 
    case 002 :
        System.out.println("It's a desktop!");
        break;
}


Here, if we pass 001, the first case matches, and the code block executes. But due to missing break, execution falls through and continues for case 002. We get the following wrong output:
It's a laptop!
It's a desktop!

Clearly, this is not the intended output. It is a result of accidentally missing-out break statements.
2. Multiple values per case not supported
There may be situations where similar processing is required for multiple case values. But the traditional switch makes to follow the fall through behaviour.
case 001:
case 002:
case 003:
System.out.println("It's an electronic gadget!");

Much improved switch accepts multiple values per case.
case 001, 002, 003 : 
        System.out.println("It's an electronic gadget!");


Enhancements to switch statements were introduced by Java 12 and then further modified by Java 13.
Let’s dive into the important features of this improved version of the Switch statement.
1. Supports multiple values per case
With multiple values being specified per case, it simplifies the code structure and eliminates the need for using fall through.
The values need to be separated by commas and break should follow the case block.
switch (itemCode) {
    case 001, 002, 003 : 
        System.out.println("It's an electronic gadget!");
        break;
        
    case 004, 005:
        System.out.println("It's a mechanical device!");
        break;
}

2. yield is used to return a value
A new keyword yield has been introduced. It returns values from a switch branch only. 
We don’t need a break after yield as it automatically terminates the switch expression.
int val = switch (code) {
    case "x", "y" :
        yield 1;
    case "z", "w" :
        yield 2;
}

3. Switch can be used as an expression
The switch can now be used as an expression. This means the switch can now return values based on our input. There is a slight change in switch syntax to accommodate this change. A switch block needs to be delimited by a semicolon. The yield keyword is used to return values. No break required with the yield statement.
Let’s have a look at a code snippet to understand these changes better.
String text = switch (itemCode) {
    case 001 : 
        yield "It's a laptop!";
    case 002 :
        yield "It's a desktop!";        
    case 003 :
        yield "It's a mobile phone!";
    default :
        throw new IllegalArgumentException(itemCode + "is an unknown device!");
}

4. Necessary to return value/exception
The switch expression is required to specify the handling of all the possible input values. Either we provide all the possible cases or specify the default one. This means, irrespective of the input value, switch expression should always return some value or explicitly throw an exception.
For instance, if the above code block is changed to –
String text = switch (itemCode) {
    case 001 : 
        yield "It's a laptop!";
    case 002 :
        yield "It's a desktop!";        
    case 003 :
        yield "It's a mobile phone!";
    // default :
    //    throw new IllegalArgumentException(itemCode + "is an unknown device!");
}

This will result in an error saying that all possible values have not been covered by the switch expression.
5. Switch with arrows 
The new arrow ⇾ syntax has been introduced for the switch. It can be used with the switch as an expression as well as a statement. 
The statements on the right side of an ⇾ are executed if an exact case matches on the left side.
On the right side of ⇾ we can have any of the following –
Statement / expression
throw statement
{} block
The main advantage of this syntax is that we don’t need a break statement to avoid the default fall-through. So the rule is, if we need fall-through, use case: else if not use case ⇾. Also note, for all case branches it should be either case: or case ⇾. It cannot be different or different cases in a switch else it results in an error.
switch (itemCode) {
    case 001 -> System.out.println("It's a laptop!");
    case 002 -> System.out.println("It's a desktop!");
    case 003,004 -> System.out.println("It's a mobile phone!");
}

As we can see in the above code, the syntax can also be used for multiple values per case. 
6. Scope
The variables declared in the traditional switch exists until the end of the switch statement. If we want the variables to have a case level scope, we can use {} introduced by the enhanced switch in Java 13.
switch (errorCode) {
    case 101: {
        // This variable exists just in this {} block
        int num = 200;
        break;
    }
    case 300: {
        // This is ok, {} block has a separate scope
        int num = 300;
        break;
    }
}

7. Preview feature 
After diving into the features related to the enhanced switch, the point noteworthy is — the enhanced switch functionality is available only as a preview feature in Java 13. This means it is not enabled by default. To use it, we need to explicitly enable it.
At compile time, add the following params to javac:
javac -- release 13 --enable-preview MyClass.java

At run time, add the following:
java --enable-preview MyClass

The enhanced switch in Java 13 provides a number of impressive features to the traditional switch. However, it is still in the experiment phase and not yet meant for use in production.
Java-Control-Flow
Java
Java-Control-Flow
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
PriorityQueue in Java
Internal Working of HashMap in Java