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
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
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
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid day";
};
System.out.println(dayName);Wednesday
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.
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.
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
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.
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
};
}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
Rewrite a month-number-to-season if-else chain as a switch expression.
Write an isWeekend(Day day) method using an exhaustive switch expression with no default.
Use yield to compute and log a discount percentage based on a customer tier, inside a switch expression.