Detailed explanation
A switch statement controls execution: it selects a branch and runs its code without producing a value of its own. A switch expression evaluates to a value that can be assigned, returned, or used inside another expression. Switch expressions became a standard feature in Java 14 after preview releases in Java 12 and 13. They are useful when several alternatives determine one result, because they avoid repeated assignments and make the relationship between a condition and its result explicit.
The important distinction is not simply “statement uses colon, expression uses arrow.” Both statements and expressions support arrow rules and traditional colon-labelled groups. With colon labels, execution can fall through into later groups unless control exits, for example through break in a statement or yield in an expression. Arrow rules do not fall through, so they do not need break. A statement such as switch (day) { case 1 -> System.out.println("Mon"); default -> System.out.println("Other"); } therefore uses arrow syntax without becoming an expression.
In the expression String name = switch (day) { case 1 -> "Mon"; case 2 -> "Tue"; default -> "Other"; };, the selected branch supplies the value assigned to name. An arrow branch containing a single expression supplies that value directly. If a branch needs multiple operations, use a block and yield, such as case 1 -> { logDay(); yield "Mon"; }. Yield exits the enclosing switch expression with a value; break does not return a value. A branch may also throw an exception instead of producing a result. The compiler checks branch result types against the surrounding context or derives a compatible result type.
A switch expression must be exhaustive: every possible selector value must be covered. For an int selector, a default branch is needed; for an enum, listing every constant can satisfy exhaustiveness without default. An ordinary Java 14 switch statement need not be exhaustive and may do nothing when no label matches. Interviewers usually expect you to separate value production, exhaustiveness, and fall-through rules rather than treating arrow syntax as the defining feature.
Key points to remember
- A statement performs actions; an expression produces a value.
- Both forms support arrow rules and colon-labelled case groups.
- Arrow rules prevent fall-through; colon groups can fall through.
- Use yield to supply a value from an expression branch block.
- Switch expressions require exhaustive coverage and compatible result types.
Common mistakes candidates make
- Claiming every switch statement requires break or that arrow syntax is exclusive to expressions.
- Using break with a value instead of yield inside a switch expression.
- Saying default is always mandatory, overlooking expressions that cover every enum constant.
Likely follow-up questions
What is the difference between yield and return?
Yield supplies a value to the enclosing switch expression, after which surrounding execution continues. Return exits the enclosing method; it cannot be used to jump out through a switch expression.
Can a switch expression use traditional colon labels?
Yes, colon-labelled groups are supported and can fall through. Every path must ultimately yield a value or complete abruptly, such as by throwing an exception.
Does an exhaustive switch expression automatically handle null?
No. In Java 14, switching on a null reference selector, such as a String or enum, throws NullPointerException, even with default; later Java versions support explicit case null handling.
Interview tip: Start with actions versus values, explain that syntax and fall-through are separate choices, and finish with a short assignment example.
