Java Switch statements and Switch Expressions (JEP 361) – Java 14.

As part of JEP 361, Java 14 has extended switch so it can be used as a statement or an expression. It was a preview language feature in Java 12 and Java 13 and became a permanent feature in Java 14.

Now lets see some examples:

Switch Statement

Before switch expressions we write switch statements as shown below. Note that only one constant is allowed per case statement followed by a colon “:“. It also need break statements to avoid fall through.

    public static Season getSeason(Month month){
        Season season = null;
        switch(month){
            case DECEMBER:
            case JANUARY:
            case FEBRUARY:
                season = Season.WINTER;
                break;
            case MARCH:
            case APRIL:
            case MAY:
                season = Season.SPRING;
                break;
            case JUNE:
            case JULY:
            case AUGUST:
                season = Season.SUMMER;
                break;
            case SEPTEMBER:
            case OCTOBER:
            case NOVEMBER:
                season = Season.FALL;
        }
        return season;
    }

    public enum Month {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER}

    public enum Season {WINTER, SPRING, SUMMER, FALL}

Switch Expressions:

If we write the same switch statement as a switch expression it will look like below. See how concise it is when compared to the above switch statement.

Note that the case can have multiple constants followed by an arrow operator “->“. Breaks are not needed here as there is no fall through.

public static Season getSeason(Month month){
Season season = switch(month){
case DECEMBER, JANUARY, FEBRUARY -> Season.WINTER;
case MARCH, APRIL, MAY -> Season.SPRING;
case JUNE, JULY, AUGUST -> Season.SUMMER;
case SEPTEMBER, OCTOBER, NOVEMBER -> Season.FALL;
};
return season;
}

Note: In a switch expression if you have multiple lines on the right of case CONSTANT ->, then we have to use yield statement to return the value as shown below. In this case if we don’t provide yield statement the compiler spits out the error message : java: switch rule completes without providing a value
(switch rules in switch expressions must either provide a value or throw)

public static Season getSeason(Month month){
Season season = switch(month){
case DECEMBER, JANUARY, FEBRUARY -> {
System.out.println(month);
yield Season.WINTER; }
case MARCH, APRIL, MAY -> Season.SPRING;
case JUNE, JULY, AUGUST -> Season.SUMMER;
case SEPTEMBER, OCTOBER, NOVEMBER -> Season.FALL;
};
return season;
}

And below are the main differences between Switch Statements and Switch Expressions:

Switch StatementsSwitch Expressions
One constant per case
case JANUARY:
case FEBRUARY:
season = Season.WINTER;
Allows multiple constants per case.
case JANUARY, FEBRUARY -> season = Season.WINTER;
Fall through after a successful case match. break statements are needed to prevent fall through.break; statements are not needed as there is no fall through.
Switch statements cannot assign the switch result to a variable. Switch expression results can be assigned to a variable. yield result; statement is needed to assign the Switch expression result to a variable if the right side has multiple statements.
colon is used for case statements.
case: result;
Both colon and arrow operator can be used but arrow operator is preferred.
case -> result;