Java 13 - Switch Expressions (JEP 354)

Enhancements for switch expressions are introduced in JDK 12 as a preview feature (JEP 325). Java 13's JEP 354 extends this switch expressions by adding a new yield keyword to return a value from switch expression. That means, a switch expression that returning a value should use yield, and a switch statement that not returning a value should use break.

Here the example of switch expression with break to return value in JDK 12:

Java12SwitchCaseBreak.java
public class Java12SwitchCaseBreak {

    public static void main(String[] args) {
        getGrade('A');
        getGrade('C');
        getGrade('D');
        getGrade('E');
        getGrade('X');
    }

    public static void getGrade(char grade) {
        System.out.print(switch (grade) {
            case 'A':
                break "Excellent";
            case 'B':
                break "Good";
            case 'C':
                break "Standard";
            case 'D':
                break "Low";
            case 'E':
                break "Very Low";
            default:
                break "Invalid";
        });

        System.out.println(getResult(grade));
    }

    public static String getResult(char grade) {
        return switch (grade) {
            case 'A', 'B', 'C':
                break "::Success";
            case 'D', 'E':
                break "::Fail";
            default:
                break "::No result";
        };
    }
}
                    

In JDK 13, you need to use yield, instead of break.

Java13SwitchCaseBreak.java
public class Java13SwitchCaseBreak {

    public static void main(String[] args) {
        getGrade('A');
        getGrade('C');
        getGrade('D');
        getGrade('E');
        getGrade('X');
    }

    public static void getGrade(char grade) {
        System.out.print(switch (grade) {
            case 'A': yield "Excellent";
            case 'B': yield "Good";
            case 'C': yield "Standard";
            case 'D': yield "Low";
            case 'E': yield "Very Low";
            default: yield "Invalid";
        });

        System.out.println(getResult(grade));
    }

    public static String getResult(char grade) {
        return switch (grade) {
            case 'A', 'B', 'C' -> "::Success";
            case 'D', 'E' -> "::Fail";
            default -> "::No result";
        };
    }
}
                    

As you can see return values using arrows from Java 12 switch expression is still supported:

return switch (grade) { case 'A', 'B', 'C' -> "::Success"; case 'D', 'E' -> "::Fail"; default -> "::No result"; };

Switch expressions are a preview feature and are disabled by default. You need to enable it by using --enable-preview option.

# compile javac --enable-preview <other flags> <source files> # run java --enable-preview <other flags> <main class>

See also: Java 12 - Switch Expressions, which contains section on how to enable preview flag in maven-compiler-plugin. Or if using gradle, in your build.gradle file:

tasks.withType(JavaCompile).each { it.options.compilerArgs.add('--enable-preview') } test { jvmArgs(['--enable-preview']) }
This article is part of What is New in Java 13 Series.

Other articles in this series: