Java 12 – Switch Expressions

switch expression

Java 12, JEP 325: Switch Expressions enhanced the traditional switch statement to support the following new features:

  • Multiple case labels
  • Switch expression returning value via break (replaced with yield in Java 13 switch expressions)
  • Switch expression returning value via label rules (arrow)

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

A traditional switch example.

JEP325.java

package com.mkyong.java12;

public class JEP325 {

    public static void main(String[] args) {

        System.out.println(getTextBefore12(1));
        System.out.println(getTextBefore12(3));
        System.out.println(getTextBefore12(6));
        System.out.println(getTextBefore12(10));

    }

    // Traditional switch statement
    private static String getTextBefore12(int number) {
        String result = "";
        switch (number) {
            case 1:
            case 2:
                result = "one or two";
                break;
            case 3:
                result = "three";
                break;
            case 4:
            case 5:
            case 6:
                result = "four or five or six";
                break;
            default:
                result = "unknown";
        };
        return result;
    }

}

Output


one or two
three
four or five or six
unknown

1. Multiple case labels

Now, we can declares multiple cases with comma-separated labels.


    private static String getTextMultipleLabels(int number) {
        String result = "";
        switch (number) {
            case 1, 2:
                result = "one or two";
                break;
            case 3:
                result = "three";
                break;
            case 4, 5, 6:
                result = "four or five or six";
                break;
            default:
                result = "unknown";
        };
        return result;
    }

2. Return value via Break

Now, this switch is an expression (returning something), also called value breaks.


    private static String getTextViaBreak(int number) {
        String result = switch (number) {
            case 1:
            case 2:
                break "one or two"; 
            case 3:
                break "three";
            case 4:
            case 5:
            case 6:
                break "four or five or six";
            default:
                break "unknown";
        };
        return result;
    }
Note
This value breaks are superseded by Java 13 yield keyword, read this Java 13 switch expressions.

4. Return value via Arrow

We also can return value via arrow or label rules.


    private static String getTextViaArrow(int number) {
        String result = switch (number) {
            case 1, 2 -> "one or two";
            case 3 -> "three";
            case 4, 5, 6 -> "four or five or six";
            default -> "unknown";
        };
        return result;
    }

5. Enable preview features

5.1 A normal javac compile will prompts the following errors:


$ javac JEP325.java
JEP325.java:39: error: multiple case labels are a preview feature and are disabled by default.
            case 1, 2:
                    ^
  (use --enable-preview to enable multiple case labels)

JEP325.java:56: error: switch expressions are a preview feature and are disabled by default.
        String result = switch (number) {
                        ^
  (use --enable-preview to enable switch expressions)
JEP325.java:75: error: switch rules are a preview feature and are disabled by default.
            case 1, 2 -> "one or two";
                      ^
  (use --enable-preview to enable switch rules)
3 errors

5.2 We need to enable the preview feature manually:


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

References

About the Author

author image
mkyong
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

avatar