Lesson · Control Flow
Modern switch: arrows, yield, and exhaustiveness
11 min readModern Switch
The arrow form removed fall-through, switch expressions turn branching into a value, yield returns from a block, and the compiler now checks that enums are covered completely.
Arrow labels: one case, one outcome
The arrow form replaces case X: plus break with case X ->, and it can never fall through. Each arm owns exactly one expression, statement or block, which makes the classic missing-break bug impossible rather than merely discouraged.
Everything a statement switch could take, the arrow form takes too, and it adds nothing new to remember beyond the shape: match, arrow, outcome. Multiple labels per arm are allowed (case 1, 2, 3 ->), which is how ranges-of-values are written before pattern matching enters the picture.
Switch lab
pick a constructArrow labels run exactly one branch and stop. The classic missing-break bug does not exist in this form, which is the main reason to write it.
Arrow labels end the fall-through era; the expression form adds exhaustiveness the compiler enforces.
Switch expressions: branching as a value
A switch can produce a value: int cups = switch (size) { case S -> 1; case M -> 2; case L -> 3; };. The arms supply the value, the semicolon ends the assignment, and this is the form that made switch useful inside expressions and stream pipelines.
Because an expression must always produce something, the compiler enforces two things the statement form never did: a default arm (or exhaustive enum coverage), and consistent value types across arms. A switch expression that might fall off the end is a compile error, not a runtime zero.
yield: values from blocks
When an arm needs more than an expression, give it a block and finish with yield value;. yield is the return of a switch expression, it must be the last thing a path does, and it is a contextual keyword: existing code with a variable or method named yield keeps compiling until it appears inside a switch expression.
The old colon form of switch expressions still exists for compatibility, using yield as well, but new code should read as arrows end to end. One rule of thumb: if you are writing case X: with a colon in 2026, you are either calling old code or writing it.
Exhaustiveness and null
For enums, the compiler checks that every constant is covered when the switch is used as an expression. Add a new constant to the enum and every exhaustive switch that forgot it stops compiling, in every project. That is the compiler doing the maintenance work interviewers love to talk about.
Classic switch throws NullPointerException when the selector is null. The arrow and expression forms have the same defect by default: null matches no case and reaches default only if default exists, otherwise it throws. Pattern matching for switch (Chapter 16) finally makes null a case you can name.
Switch expressions must be exhaustive
A statement switch may do nothing when nothing matches. An expression switch must produce a value on every path, so a missing default or an uncovered enum constant is a compile error. The strictness is the feature: adding an enum constant breaks every place that should handle it.
Interview angles
Why does the arrow form exist?+
To delete fall-through and the break bookkeeping with it: one case, one arrow, one outcome that runs and stops. It also made switch usable as an expression, which the colon form was not designed for.
What is yield for?+
Producing the value of a switch expression from a block arm. It is a contextual keyword, the last action on its path, and the replacement for the old pattern of assigning to a variable inside colon cases.
What does exhaustiveness mean for a switch expression?+
The compiler proves a value exists on every path: a default arm, or full coverage of an enum or sealed hierarchy. Add an enum constant later and every incomplete switch fails to compile, which surfaces the work at build time instead of at 3 a.m.