Switch Expressions
BeginnerSwitch expressions (Java 14+) return values, use arrow syntax, and eliminate fall-through bugs from traditional switch statements.
Overview
Traditional switch statements in Java have long been error-prone due to fall-through, verbose break statements, and inability to return a value. Switch expressions, finalized in Java 14, address all of these. They can produce a value, support arrow (→) case syntax that prevents fall-through, and use yield to return a value from a block body. Combined with pattern matching (Java 21), they become the primary tool for type-safe dispatch.
Arrow Switch Expression
The arrow form (case X -> expression) is the preferred modern syntax. Each arm is a single expression; there is no fall-through. The switch expression itself evaluates to the selected arm's value.
This eliminates missing break bugs and makes the intent immediately clear.
// Traditional switch statement (error-prone)
int day = 3;
String dayName;
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
// ...
default: dayName = "Unknown";
}
// Modern switch expression
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
default -> "Weekend";
};
System.out.println(dayName); // WednesdayBlock Bodies and yield
When a case arm needs multiple statements, use a block with yield to return the value. yield is the switch-expression equivalent of return — it exits the switch and provides the result.
int numLetters = switch (day) {
case "Monday", "Friday", "Sunday" -> 6;
case "Tuesday" -> 7;
case "Thursday", "Saturday" -> 8;
case "Wednesday" -> {
System.out.println("Longest day name!");
yield 9; // exit with value
}
default -> throw new IllegalArgumentException("Unknown: " + day);
};Multiple Labels per Case
Both the traditional and arrow forms support multiple comma-separated labels per case. This replaces the old fall-through pattern used to share code across cases.
Switch expressions must be exhaustive — every possible value must be covered, either explicitly or with default.
Season season = Season.SPRING;
String description = switch (season) {
case SPRING, SUMMER -> "Warm";
case AUTUMN, FALL -> "Cool";
case WINTER -> "Cold";
};
// Enum switch — no default needed when all constants covered
int quarters = switch (season) {
case SPRING -> 1;
case SUMMER -> 2;
case AUTUMN, FALL -> 3;
case WINTER -> 4;
};Key Points to Remember
- Switch expressions return a value; switch statements do not.
- Arrow cases (case X ->) prevent fall-through and require no break.
- Use yield to return a value from a multi-statement block arm.
- Multiple labels per case: case A, B, C -> ... replaces fall-through patterns.
- Switch expressions must be exhaustive — all inputs must be covered.
Practice Switch Expressions in the Playground
Run and modify code directly in your browser - no setup needed.
Interview Questions
Sign in to ask AriaWhat is the difference between a switch statement and a switch expression in Java?
When do you need yield in a switch expression?
Why can arrow-case switch expressions not fall through?
What does it mean for a switch expression to be exhaustive?
How would you refactor a complex if-else chain into a switch expression?
Ask Aria about Switch Expressions
Your personal AI tutor — ask anything about this concept