Main Tutorials

Java 13 – Switch Expressions

switch expression

In Java 13, the JEP 354: Switch Expressions extends the previous Java 12 Switch Expressions by adding a new yield keyword to return a value from the switch expression.

P.S Switch expressions are a preview feature and are disabled by default.

Note
This is a standard feature in Java 14.

1. No more value breaks!

1.1 Java 12 value breaks syntax is no longer compiled in Java 13, uses yield instead.


	// value breaks are superseded by Java 13 'yield' statements.
	private static int getValueViaBreak(String mode) {
        int result = switch (mode) {
            case "a":
            case "b":
                break 1;
            case "c":
                break 2;
            case "d":
            case "e":
            case "f":
                break 3;
            default:
                break -1;
        };
        return result;
    }

1.2 In Java 13, we can use yield to return a value.


	private static int getValueViaYield(String mode) {
        int result = switch (mode) {
            case "a", "b":
                yield 1;
            case "c":
                yield 2;
            case "d", "e", "f":
                // do something here...
                System.out.println("Supports multi line block!");
                yield 3;
            default:
                yield -1;
        };
        return result;
    }

1.3 The label rules or arrows from a switch expression (Java 12) is still supported in Java 13.


	private static int getValueViaArrow(String mode) {
        int result = switch (mode) {
            case "a", "b" -> 1;
            case "c" -> 2;
            case "d", "e", "f" -> {
                // do something here...
                System.out.println("Supports multi line block!");
                yield 3;
            }
            default -> -1;
        };
        return result;
    }

2. Java 13 Switch Expressions

A full switch expression example in Java 13.

JEP354.java

package com.mkyong.java13;

public class JEP354 {

    public static void main(String[] args) {

        System.out.println(getValueViaYield("a"));
        System.out.println(getValueViaYield("c"));
        System.out.println(getValueViaYield("e"));
        System.out.println(getValueViaYield("z"));

    }

    // Traditional switch
    private static int getValueBefore12(String mode) {
        int result;
        switch (mode) {
            case "a":
            case "b":
                result = 1;
                break;
            case "c":
                result = 2;
                break;
            case "d":
            case "e":
            case "f":
                result = 3;
                break;
            default:
                result = -1;
        }
        ;
        return result;
    }

    // Java 12, multiple comma-separated labels
    private static int getValueMultipleLabels(String mode) {
        int result;
        switch (mode) {
            case "a", "b":
                result = 1;
                break;
            case "c":
                result = 2;
                break;
            case "d", "e", "f":
                result = 3;
                break;
            default:
                result = -1;
        }
        ;
        return result;
    }

    // Java 13, value breaks are superseded by 'yield' statements
    // Java 12, switch expression returning value via break
    /*private static int getValueViaBreak(String mode) {
        int result = switch (mode) {
            case "a":
            case "b":
                break 1;
            case "c":
                break 2;
            case "d":
            case "e":
            case "f":
                break 3;
            default:
                break -1;
        };
        return result;
    }*/

    // Java 12, switch expression returns a value via label rules (arrow)
    private static int getValueViaArrow(String mode) {
        int result = switch (mode) {
            case "a", "b" -> 1;
            case "c" -> 2;
            case "d", "e", "f" -> {
                // do something here...
                System.out.println("Supports multi line block!");
                yield 3;
            }
            default -> -1;
        };
        return result;
    }

    // Java 13, switch expression returns a value via yield
    private static int getValueViaYield(String mode) {
        int result = switch (mode) {
            case "a", "b":
                yield 1;
            case "c":
                yield 2;
            case "d", "e", "f":
                // do something here...
                System.out.println("Supports multi line block!");
                yield 3;
            default:
                yield -1;
        };
        return result;
    }

}

Output


1
2
Supports multi line block!
3
-1

3. Enable preview features

3.1 A normal javac compile will prompts the following errors:


$ javac JEP354.java
D:\test>javac JEP354.java
JEP354.java:39: error: multiple case labels are a preview feature and are disabled by default.
            case "a", "b":
                      ^
  (use --enable-preview to enable multiple case labels)
JEP354.java:76: error: switch expressions are a preview feature and are disabled by default.
        int result = switch (mode) {
                     ^
  (use --enable-preview to enable switch expressions)
JEP354.java:77: error: switch rules are a preview feature and are disabled by default.
            case "a", "b" -> 1;
                          ^
  (use --enable-preview to enable switch rules)
3 errors

3.2 We need to enable the preview feature manually:


javac --enable-preview --release 13 Example.java
java --enable-preview Example

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments