Javaswitch Expressions

Java switch Expressions

From Statement to Expression

You already know the classic switch statement — it checks a variable against a list of cases and runs matching code, falling through to the next case unless you add a break. Starting with Java 14, the language gained a second, more modern form: the switch expression. Instead of just running statements, a switch expression can produce a value directly, which you can assign to a variable, return from a method, or pass as an argument.

The new syntax uses an arrow (->) instead of a colon, and it comes with a very welcome side effect: no more accidental fall-through, and no more break statements.

Arrow Syntax

Java
String result = switch (value) {
    case 1 -> "One";
    case 2 -> "Two";
    case 3 -> "Three";
    default -> "Unknown";
};
  • Each case runs only its own branch — there is no fall-through to the next case.

  • The whole switch produces a value, which is assigned straight to result.

  • default is required unless every possible value is already covered.

Before / After: The Same Logic, Two Styles

Here is a day-of-week name lookup written the classic way, using a statement:

Classic switch statement

Java
String dayName;
switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    default:
        dayName = "Invalid day";
        break;
}
System.out.println(dayName);

Now the same logic as a modern switch expression:

Modern switch expression

Java
String dayName = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Invalid day";
};
System.out.println(dayName);
Wednesday
Tip
The expression form is shorter, removes the risk of forgetting a break, and makes it obvious at a glance that every branch is producing the same kind of value.
Multiple Labels Per Case

You can group several values into a single case using commas — no need to stack case labels one per line anymore.

Java
String type = switch (grade) {
    case 'A', 'B', 'C' -> "Pass";
    case 'D', 'F' -> "Fail";
    default -> "Invalid grade";
};
yield — For Multi-Statement Branches

Sometimes a single expression isn't enough — you need a small block of code to compute the result. In that case, use braces and the yield keyword to specify the value the branch produces.

Java
int base = 3;
int score = switch (base) {
    case 1 -> 10;
    case 2 -> 20;
    default -> {
        int computed = base * 10;
        System.out.println("Computed a fallback score");
        yield computed;
    }
};
System.out.println(score);
Computed a fallback score
30
Note
yield is to a switch expression what return is to a method — it hands back the value for that branch. You only need it inside a braced block; single-expression arrows (case 1 -> 10;) don't need it at all.
Exhaustiveness

A switch expression must always produce a value for every possible input — the compiler enforces this. For most types you satisfy this with a default branch. But when you switch over an enum or a sealed type, and your cases already cover every constant or every permitted subtype, the compiler considers the expression exhaustive and a default branch becomes optional.

Java
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }

static boolean isWeekend(Day day) {
    return switch (day) {
        case SAT, SUN -> true;
        case MON, TUE, WED, THU, FRI -> false;
        // no default needed — every Day constant is covered
    };
}
Warning
If you switch on an enum or sealed type and DON'T cover every case (and don't supply a default), the code will not compile — the compiler tells you the switch expression is not exhaustive. This is a genuine safety net: if someone later adds a new enum constant, every switch expression that forgot to handle it will fail to build instead of silently misbehaving at runtime.
switch Statement vs switch Expression

Aspect

switch statement

switch expression

Produces a value

No

Yes

Syntax

case X:

case X ->

Fall-through

Yes, unless break is used

Never

Multiple values per case

Stacked case labels

Comma-separated list

Multi-statement branch

Just write statements

Braced block + yield

Exhaustiveness check

Not enforced

Enforced by the compiler

Practice Exercises
  1. Rewrite a month-number-to-season if-else chain as a switch expression.

  2. Write an isWeekend(Day day) method using an exhaustive switch expression with no default.

  3. Use yield to compute and log a discount percentage based on a customer tier, inside a switch expression.