[Java] Let's take a look at Switch Expressions (Preview) of JDK 13.

Five important features of OpenJDK 13

350: Dynamic CDS Archives 351: ZGC: Uncommit Unused Memory 353: Reimplement the Legacy Socket API 354: Switch Expressions (Preview) 355: Text Blocks (Preview)

Among the above, today I would like to take a look at "Switch Expressions (Preview)".

Switch Expressions (Preview) --Is it Preview in JDK 12 or Preview in 13? Since it is --Preview, it seems that it cannot be used unless the --enable-preview option is given.

・ Arrow labels


static void howMany(int k) {
    switch (k) {
        case 1  -> System.out.println("one");
        case 2  -> System.out.println("two");
        default -> System.out.println("many");
    }
}

I used to use the conventional "case L:", but now I can use it like "case L->". However, if you write something like "case L->", there is no full through, so you don't have to write break.

When calling, it will be as follows.

howMany(1);
howMany(2);
howMany(3);
result

one
two
many

・ Switch expressions

switch is now available in expression.


static void howMany(int k) {
    System.out.println(
        switch (k) {
            case  1 -> "one"
            case  2 -> "two"
            default -> "many"
        }
    );
}

So you can also assign the result of a switch statement to a variable.


T result = switch (arg) {
    case L1 -> e1;
    case L2 -> e2;
    default -> e3;
};

・ Yielding a value

The "yield" keyword has been added.


int j = switch (day) {
    case MONDAY  -> 0;
    case TUESDAY -> 1;
    default      -> {
        int k = day.toString().length();
        int result = f(k);
        yield result; //Set the return value of the switch statement
    }
};

"Yield" can also be used in switch statements of "case L:" expressions.


int result = switch (s) {
    case "Foo": 
        yield 1;
    case "Bar":
        yield 2;
    default:
        System.out.println("Neither Foo nor Bar, hmmm...");
        yield 0;
};

reference

Recommended Posts

[Java] Let's take a look at Switch Expressions (Preview) of JDK 13.
Let's take a look at the screen of Quant Analyzer!
Let's take a look at the functions of Keycloak's management console (administrator edition)
Let's take a look at the Hotspot JVM startup procedure
Take a look at Kotlin from an Effective Java perspective
Let's take a look at the functions of Keycloak's management console (user edition), user account service
Let's create a TODO application in Java 5 Switch the display of TODO
Name a group of regular expressions (Java)
Let's make a robot! "A simple demo of Java AWT Robot"
[Java] Summary of regular expressions
I tried to take a look at the flow of Android development environment construction with Android Studio
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
I took a look at the resources of Azure Container Instance
Let's create a TODO application in Java 1 Brief explanation of MVC
A quick look back at Java over the last five years
Sort a List of Java objects
A brief description of JAVA dependencies
The origin of Java lambda expressions
A little understanding of lambda expressions
Let's express the result of analyzing Java bytecode with a class diagram