When and how do you use 'yield' in a switch expression?
answer
- yield = value out of a block branch
- Single-expression arrow branch needs no yield
- Colon-form expression: yield mandatory per branch
- yield != return (return exits the method)
- Block branch must yield or throw
basics
~10 sUse 'yield' to return a value from a switch branch that is a block ({ ... }) and needs more than one statement. Single-expression arrow branches don't need it; they return their value directly.
solid answer
~50 sIn a switch expression, a branch that is a single expression returns that value automatically: 'case A -> 1;'. But when a branch needs multiple statements you wrap them in a block, and a block doesn't implicitly produce a value — so you use 'yield' to specify the result: 'case A -> { var x = compute(); yield x * 2; }'. 'yield' is also required for every non-empty branch in the colon (':') form of a switch expression, because there the body is a statement group rather than a single expression. 'yield' produces a value for the enclosing switch expression and is distinct from 'return' (which exits the whole method) and 'break' (which has no value). Every reachable branch of a switch *expression* must produce a value (or throw), so a block branch that neither yields nor throws is a compile error.
go deeper
Knows yield returns the value from a multi-statement block branch and that simple arrow branches don't need it.
Distinguishes yield from return and break, and knows the colon-form expression requires yield per branch plus the must-yield-or-throw rule.
Explains yield as a contextual keyword, reasons about exhaustiveness/completeness compile errors, and reviews code for accidental 'return' inside switch expressions.
Sets team conventions favoring small expression branches over heavy yield blocks, and understands the language-design rationale for a separate yield keyword vs reusing break/return.
## The problem yield solves A switch **expression** must evaluate to a value. With the arrow form, a single-expression branch supplies that value directly: ```java int n = switch (size) { case SMALL -> 1; // the expression '1' is the branch's value case MEDIUM -> 2; default -> 3; }; ``` But sometimes a branch needs several statements — logging, a loop, a local variable. You then use a **block** `{ ... }`. A block is a sequence of statements and, by itself, does **not** produce a value. So how does the switch get its result from a block branch? With the keyword **`yield`**. ## Using yield `yield <expression>;` sets the value of the enclosing switch expression for that branch and ends the branch: ```java int price = switch (item) { case BOOK -> 10; case GADGET -> { int base = lookupBase(item); // multiple statements need a block int tax = base / 5; yield base + tax; // this is the branch's value } default -> 0; }; ``` ## yield vs return vs break These three are easy to confuse: - **`yield x`** — produces value `x` for the *switch expression*. Control continues after the switch. - **`return x`** — exits the *entire enclosing method*, not just the switch. - **`break`** — exits a loop or a switch *statement*; it carries **no value** and is not used in arrow expressions. Using `return` inside a switch expression that is being assigned would skip the assignment entirely and leave the method — usually a bug. ## The colon form The older colon (`:`) syntax can also be a switch *expression*, and there `yield` is mandatory for each populated branch because the body is a group of statements, not a single expression: ```java int n = switch (day) { case MONDAY: case FRIDAY: yield 6; default: yield 0; }; ``` ## Completeness requirement Because the whole construct must yield a value, **every reachable branch** of a switch expression must either `yield` a value or `throw`. A block branch that falls off the end without yielding (and without throwing) is a **compile-time error** — the compiler will not let you produce a switch expression with a value-less path. ## Note: yield is a contextual keyword `yield` is a *contextual* keyword: it only acts as a keyword inside a switch; you can still have a variable or method named `yield` elsewhere (though it's discouraged for clarity).
- What happens if a block branch ends without a yield and without throwing?Compile-time error. A switch expression must produce a value on every reachable path, so a value-less block branch is rejected by the compiler.
- Is 'yield' a reserved word that breaks old code using it as an identifier?No. 'yield' is a contextual keyword — it only acts as a keyword inside a switch. Existing variables or methods named 'yield' still compile, though renaming them improves clarity.
saying these in an interview costs you the question
- Using 'return' to produce a switch-expression value (it exits the method instead)
- Using 'break value' — that's not valid; the keyword is 'yield'
- Putting yield on a single-expression arrow branch (unnecessary and a compile error)
- Forgetting that every block branch must yield or throw