Switch expressions
A switch expression picks a value by matching a selector against labels. It must cover every case, it yields a result, and it does not fall through.
Detailed Explanation
The old switch statement was a statement with a trap: if you forgot break, execution fell into the next case. Switch expressions (standard since Java 14, then grown with pattern matching) flip the default. You write yield or an arrow label, the match produces a value, and cases do not fall through.
Arrow labels (case MONDAY -> "weekday") are the form you want for a single value or a block that yields. Colon labels with yield exist for migration. Multiple labels share a body: case SATURDAY, SUNDAY -> "weekend". The selector can be an int, String, enum, or, with pattern matching, a reference type you deconstruct.
Exhaustiveness is the compiler's job. On an enum or a sealed type, listing every constant or permitted subtype is enough. If you omit one, compilation fails. A default branch covers the rest, including unexpected enum constants added later and loaded from older compiled callers. For String and int, default (or a complete set of known values plus default) is required because the domain is open.
Pattern switches bind variables. case Circle c -> c.r() both tests the type and introduces c. Guarded patterns (case User u when u.admin()) add a boolean. The first matching label wins. null is its own case in modern switches; without case null, a null selector throws NullPointerException, which matches the old statement.
You reach for a switch expression when one value maps to another and the set of cases is known. You keep an if chain for unrelated boolean tests. You keep polymorphism when the operation belongs on the type and new subtypes should not force you to edit a central switch. Sealed types plus switch is the middle path: a closed family, a compiler-checked match.
The cost of a huge switch is the same as a huge if else chain: every new case edits one place. That is acceptable when the set is closed and the mapping is the feature. It is a smell when you have five such switches on the same enum; the enum should grow methods, or you should use a map.
Code Examples
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
public class Days {
static String kind(Day day) {
return switch (day) {
case SAT, SUN -> "weekend";
case MON, TUE, WED, THU, FRI -> "weekday";
};
}
public static void main(String[] args) {
System.out.println(kind(Day.SAT)); // prints: weekend
System.out.println(kind(Day.WED)); // prints: weekday
}
}public class Labels {
static int digits(Object value) {
return switch (value) {
case null -> 0;
case String s when s.isBlank() -> 0;
case String s -> s.length();
case Integer n -> String.valueOf(n).length();
default -> -1;
};
}
public static void main(String[] args) {
System.out.println(digits("hi")); // prints: 2
System.out.println(digits(100)); // prints: 3
System.out.println(digits(null)); // prints: 0
}
}Common Gotchas
A non-exhaustive switch expression does not compile
add the missing enum constant or a `default`.
`case null` is not implied
without it, `switch` on a null selector throws `NullPointerException`.
Arrow cases do not fall through
to share a body, list labels together (`case A, B ->`).
Pattern order matters
a bare `case String s` above a guarded `case String s when ...` makes the guard unreachable.