Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Switch expression with arrow rules

In Java SE 21, a `switch` expression selects a value. Arrow rules prevent fall-through and let several enum constants share one `case` label.

Version: Java SE 21

Syntax

ResultType result = switch (selector) {
    case CONSTANT_A -> expressionA;
    case CONSTANT_B, CONSTANT_C -> expressionBC;
};

A switch expression with arrow rules first evaluates the selector in parentheses. When a case matches, the value of the expression after -> becomes the value of the whole switch. You can use it to initialize a variable. The semicolon after the rule's expression ends the rule; the semicolon after } ends the variable declaration.

In case TEST, STAGING ->, the comma means either constant selects the same rule. Execution does not fall through to the next rule, so no break is needed. A switch statement can also use arrows, but it does not produce a value; the arrow alone does not make a switch an expression. Colon-style groups (case ...:) can fall through. You cannot mix the two forms in one switch.

A switch expression must be exhaustive. If the labels cover every constant of an enum, no default is required. If you add a constant and recompile without covering it or adding a default, compilation fails. In Java SE 21, a null selector throws NullPointerException when there is no case null; default alone does not handle null.

A plain expression after -> does not need yield. If you use a block after the arrow, yield value; supplies a result to the enclosing switch expression, not to the calling method. A rule may also throw an exception with throw. A rule block cannot simply reach its closing } without supplying a result.

Examples

public class Main {
    public static void main(String[] args) {
        enum Environment { DEV, TEST, STAGING, PROD }

        for (Environment environment : Environment.values()) {
            String profile = switch (environment) {
                case DEV -> "local";
                case TEST, STAGING -> "pre-release";
                case PROD -> "live";
            };
            int retries = switch (environment) {
                case DEV -> 0;
                case TEST, STAGING -> 1;
                case PROD -> 3;
            };
            boolean debug = switch (environment) {
                case DEV -> true;
                case TEST, STAGING -> true;
                case PROD -> false;
            };
            System.out.println(environment + ": " + profile + ", " + retries + ", " + debug);
        }
    }
}

Result

DEV: local, 0, true
TEST: pre-release, 1, true
STAGING: pre-release, 1, true
PROD: live, 3, false

Three `switch` expressions use the same local `enum`. Each covers all four constants and initializes one variable: `profile`, `retries`, or `debug`. `TEST` and `STAGING` produce the same three values but appear on separate lines.

Related lessons

  • Java · Lesson 9 · Advanced · Version: 2

Sources