Java Switch Expressions: How the yield Keyword Produces Values
Learn how the Java yield keyword produces values from switch expressions, when to use it, and how it differs from the return statement.

yield is the keyword Java uses inside switch expressions to produce a value from a branch. Switch expressions became a standard feature in Java 14. With arrow syntax, a single expression can be the branch value; when a branch needs more than one statement, or when you use the traditional colon syntax, yield supplies the result.
What yield Does in a Switch Expression
A switch expression is an expression, so when it completes normally it must produce a value. A branch can be a single expression with arrow syntax:
int quarter = switch (month) {
case "january", "february", "march" -> 1;
case "april", "may", "june" -> 2;
case "july", "august", "september" -> 3;
default -> 4;
};When a branch needs multiple statements, wrap it in a block and use yield to supply 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;
}
};yield terminates that branch and gives the value to the enclosing expression. If a block branch can complete normally, it must reach a yield; a branch may instead complete abruptly, for example by throwing or returning from the enclosing method.
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 can be assigned, passed to a method, or used in a larger expression. A switch expression must be exhaustive; for enum types, the compiler verifies that every constant is covered or a default branch exists.
Using yield with Colon Syntax
Traditional colon labels are still valid inside a switch expression. 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;
};A bare break; does not provide a value to the switch expression, so it is not a replacement for yield.
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. yield may appear anywhere inside the block, but every normal completion 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 when the enclosing method has a compatible return type, 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 lets execution continue 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; because the arrow form expects an expression, and yield is a statement. Use either the expression form or the block form, not both.
Another issue is that yield is a restricted identifier. In a branch body, yield is parsed as the statement that produces the value, so using yield as an ordinary identifier in the same code can be confusing and may fail to compile in ambiguous positions. When upgrading a codebase, inspect existing uses of yield as an identifier and rename them if the compiler rejects them.
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. Switch expressions were 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 inspect existing uses of yield as an identifier, because the restricted identifier rule can affect compilation after the upgrade.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.