Java Switch Yield: How Switch Expressions Produce Values
java switch yield: Learn how the yield keyword produces values from Java switch expressions, when to use it, and how it differs from the return statement.
Java switch yield refers to the yield keyword used inside switch expressions to produce a value from a branch. Java 14 introduced switch expressions as a standard feature, and yield is how you supply the result when a branch needs more than a single expression, or when you use the traditional colon syntax.
What yield Does in a Switch Expression
A switch expression must evaluate to a value, and every branch must produce one. The arrow syntax works when each branch is a single expression:
int quarter = switch (month) { case "january", "february", "march" -> 1; case "april", "may", "june" -> 2; case "july", "august", "september" -> 3; default -> 4; };
The moment a branch needs multiple statements, you wrap it in a block and use yield to produce the value:
int quarter = switch (month) { case "january", "february", "march" -> 1; case "april", "may", "june" -> 2; default -> { int days = getDaysInMonth(month); yield days > 30 ? 4 : 3; } };
The yield statement terminates the branch and supplies the value to the enclosing expression. Every execution path through a block branch must reach a yield.
Switch Expression vs Switch Statement
Before switch expressions, the switch statement could only perform actions; it could not produce a value. You had to assign a variable inside each branch and break out:
int quarter; switch (month) { case "january": case "february": case "march": quarter = 1; break; default: quarter = 4; }
The switch expression changes this. It is an expression, so it can be assigned, passed to a method, or used in a larger expression. Every branch must yield a value, and the compiler enforces exhaustiveness. When switching on an enum, the compiler verifies that all constants are covered or a default branch exists.
Using yield with Colon Syntax
The colon syntax from the traditional switch statement remains valid inside a switch expression, but break no longer works as before. Instead of breaking out, you use yield to produce the value and terminate the branch:
int quarter = switch (month) { case "january": case "february": case "march": yield 1; case "april": case "may": case "june": yield 2; default: yield 4; };
Each yield statement terminates the branch and supplies the value to the enclosing expression. Using break here is a compile error—the switch expression requires a value, and break does not provide one.
Using yield Inside Arrow-Style Blocks
The arrow syntax supports two forms. The first is a single expression, which implicitly becomes the branch value:
int quarter = switch (month) { case "january", "february", "march" -> 1; default -> 4; };
The second form is a block, where yield is required to produce the value:
int quarter = switch (month) { case "january", "february", "march" -> 1; default -> { logUnknownMonth(month); yield 4; } };
The block form is useful when a branch requires intermediate computation, logging, or validation before producing its result. The yield statement can appear anywhere inside the block, and every execution path must reach one.
yield vs return in Switch Expressions
A common confusion is using return inside a switch expression. return exits the enclosing method, not the switch. Consider:
int getQuarter(String month) { int quarter = switch (month) { case "january" -> 1; default -> { return 4; // exits the method, not the switch } }; System.out.println("Quarter: " + quarter); return quarter; }
This compiles only if the enclosing method returns int, but the behavior differs: the return exits the method entirely, skipping the logging line after the switch. yield produces the value for the switch expression and continues execution after it.
The distinction matters in methods that do more than return the switch result:
int getQuarter(String month) { int quarter = switch (month) { case "january" -> 1; default -> { logUnknownMonth(month); yield 4; } }; System.out.println("Quarter computed: " + quarter); return quarter; }
With yield, the logging line executes after the switch completes. With return, it does not.
Common Mistakes with yield
One frequent error is mixing yield with the arrow syntax's single-expression form. You cannot write case "january" -> yield 1;—the arrow form expects an expression, and yield is a statement. Use either the expression form or the block form, not both.
Another mistake is forgetting that yield is a restricted identifier. In Java 14 and later, yield cannot be used as a variable, method, or type name in a compilation unit that contains a switch expression. Code that used yield as a variable name before Java 14 will fail to compile when the language level is updated.
A third issue is missing exhaustiveness. A switch expression over an enum must cover all constants or include a default branch. The compiler enforces this, and the error message points to the missing case.
Compatibility and Version Requirements
Switch expressions and the yield keyword are standard since Java 14. If your project targets an earlier Java version, the compiler rejects switch expressions. The arrow syntax without blocks was previewed in Java 12 and became standard in Java 14, so release 14 is the safe baseline.
When migrating a codebase, check the maven.compiler.release setting or the equivalent --release flag in your build configuration. The switch expression syntax, including yield, requires release 14 or higher. Also verify that no existing code uses yield as an identifier, since the restricted identifier rule can break compilation after the upgrade.