00:00

Switch Expressions in Java (Standard Since Java 14)

Last updated: · Standard since Java 14

A switch expression is a switch that produces a value. With arrow labels (case X ->) there is no fall-through, one case can list several constants, and yield returns a value from a block. Switch expressions were previewed in Java 12 and 13 and became standard in Java 14 (JEP 361).

This tutorial covers the syntax, exhaustiveness checking and the differences from the classic switch statement.

1. Why Were Switch Expressions Added?

The classic switch statement falls through from one case to the next unless you remember break, which causes subtle bugs. Assigning a value from a switch also required a variable declared before it. Switch expressions remove both problems and let the compiler check that every enum constant is handled.

2. Syntax

Syntax.java
int price = switch (size) {
    case S, M -> 499;                 // several constants in one label
    case L    -> 549;
    case XL   -> {
        int base = 549;
        yield base + 50;              // yield returns a value from a block
    }
};                                    // a switch expression ends with ;
FeatureClassic switch statementSwitch expression
Produces a valuenoyes
Fall-throughyes, unless breakno with arrow labels
Multiple constants per casestacked labelscase A, B ->
Must cover all valuesnoyes (exhaustive)

3. Practical Examples

Example 1: Arrow labels and yield

SwitchExpressionBasics.java
import java.time.DayOfWeek;

public class SwitchExpressionBasics {
    enum Size { S, M, L, XL }

    public static void main(String[] args) {
        // switch returns a value; arrow labels never fall through
        for (DayOfWeek day : DayOfWeek.values()) {
            String type = switch (day) {
                case SATURDAY, SUNDAY -> "weekend";
                case FRIDAY -> "half day";
                default -> "working day";
            };
            System.out.println(day + " -> " + type);
        }

        // yield returns a value from a block
        Size size = Size.XL;
        int price = switch (size) {
            case S, M -> 499;
            case L -> 549;
            case XL -> {
                int base = 549;
                yield base + 50;        // extra fabric charge
            }
        };
        System.out.println(size + " costs " + price);
    }
}
Output
MONDAY -> working day
TUESDAY -> working day
WEDNESDAY -> working day
THURSDAY -> working day
FRIDAY -> half day
SATURDAY -> weekend
SUNDAY -> weekend
XL costs 599

Example 2: The compiler checks exhaustiveness

A switch expression over an enum must handle every constant or have a default. This file is meant to fail.

MissingCase.java
public class MissingCase {
    enum Light { RED, AMBER, GREEN }

    static int seconds(Light light) {
        // GREEN is missing, and there is no default
        return switch (light) {
            case RED -> 60;
            case AMBER -> 5;
        };
    }

    public static void main(String[] args) {
        System.out.println(seconds(Light.RED));
    }
}
Compiler output
MissingCase.java:6: error: the switch expression does not cover all possible input values
        return switch (light) {
               ^
1 error

4. How a Switch Expression Is Evaluated

Evaluating a switch expression The selector is evaluated once, the matching case label is found, its expression or yield produces the value, and the value is assigned. No other case runs. selector size = XL matching label case XL -> block + yield base + 50 result price = 599
Figure 1: Exactly one branch runs and it must produce a value.

5. Important Points and Best Practices

  • Prefer arrow labels; mixing arrow and colon labels in one switch is not allowed.
  • Avoid default for enums when you want the compiler to flag new constants.
  • yield is only needed in block bodies and in colon-style switch expressions.
  • A null selector still throws NullPointerException unless you use pattern matching with case null (Java 21).

6. Summary

  • Switch expressions became standard in Java 14 (JEP 361).
  • Arrow labels remove fall-through and allow several constants per case.
  • yield returns a value from a block.
  • Switch expressions must be exhaustive, which the compiler checks.

Related Tutorials

7. References