Switch Expressions in Java (Standard Since Java 14)
A switch expression is a switch that produces a value. With arrow labels (case X ->) there is no fall-through, one case can list several constants, and yield returns a value from a block. Switch expressions were previewed in Java 12 and 13 and became standard in Java 14 (JEP 361).
This tutorial covers the syntax, exhaustiveness checking and the differences from the classic switch statement.
1. Why Were Switch Expressions Added?
The classic switch statement falls through from one case to the next unless you remember break, which causes subtle bugs. Assigning a value from a switch also required a variable declared before it. Switch expressions remove both problems and let the compiler check that every enum constant is handled.
2. Syntax
int price = switch (size) {
case S, M -> 499; // several constants in one label
case L -> 549;
case XL -> {
int base = 549;
yield base + 50; // yield returns a value from a block
}
}; // a switch expression ends with ;
| Feature | Classic switch statement | Switch expression |
|---|---|---|
| Produces a value | no | yes |
| Fall-through | yes, unless break | no with arrow labels |
| Multiple constants per case | stacked labels | case A, B -> |
| Must cover all values | no | yes (exhaustive) |
3. Practical Examples
Example 1: Arrow labels and yield
import java.time.DayOfWeek;
public class SwitchExpressionBasics {
enum Size { S, M, L, XL }
public static void main(String[] args) {
// switch returns a value; arrow labels never fall through
for (DayOfWeek day : DayOfWeek.values()) {
String type = switch (day) {
case SATURDAY, SUNDAY -> "weekend";
case FRIDAY -> "half day";
default -> "working day";
};
System.out.println(day + " -> " + type);
}
// yield returns a value from a block
Size size = Size.XL;
int price = switch (size) {
case S, M -> 499;
case L -> 549;
case XL -> {
int base = 549;
yield base + 50; // extra fabric charge
}
};
System.out.println(size + " costs " + price);
}
}
MONDAY -> working day TUESDAY -> working day WEDNESDAY -> working day THURSDAY -> working day FRIDAY -> half day SATURDAY -> weekend SUNDAY -> weekend XL costs 599
Example 2: The compiler checks exhaustiveness
A switch expression over an enum must handle every constant or have a default. This file is meant to fail.
public class MissingCase {
enum Light { RED, AMBER, GREEN }
static int seconds(Light light) {
// GREEN is missing, and there is no default
return switch (light) {
case RED -> 60;
case AMBER -> 5;
};
}
public static void main(String[] args) {
System.out.println(seconds(Light.RED));
}
}
MissingCase.java:6: error: the switch expression does not cover all possible input values
return switch (light) {
^
1 error
4. How a Switch Expression Is Evaluated
5. Important Points and Best Practices
- Prefer arrow labels; mixing arrow and colon labels in one switch is not allowed.
- Avoid
defaultfor enums when you want the compiler to flag new constants. yieldis only needed in block bodies and in colon-style switch expressions.- A
nullselector still throwsNullPointerExceptionunless you use pattern matching withcase null(Java 21).
6. Summary
- Switch expressions became standard in Java 14 (JEP 361).
- Arrow labels remove fall-through and allow several constants per case.
yieldreturns a value from a block.- Switch expressions must be exhaustive, which the compiler checks.
Related Tutorials
- Switch Statement in Java
- Pattern Matching for switch
- Enum in Java
- Pattern Matching for switch (Java 21)
7. References
Frequently Asked Questions (FAQ)
Q1. What is the difference between a switch statement and a switch expression?
A switch expression produces a value, must be exhaustive and, with arrow labels, never falls through. A switch statement performs actions and needs break to stop fall-through.
Q2. What does yield do?
It returns a value from a block inside a switch expression, similar to return in a method.
Q3. Which Java version made switch expressions standard?
Java 14, through JEP 361, after previews in Java 12 and 13.
Q4. Does a switch expression need a default branch?
Only if the listed cases do not cover all possible values. For an enum with every constant listed, no default is needed.