Quick revision
The short answer
It prints TueWed. With colon-style cases, execution jumps to the matching case and then falls through into every following case until it meets a break, so case 2 prints Tue and carries on into case 3, which prints Wed and breaks. As a switch expression we write String name = switch (day) { case 1 -> "Mon"; case 2 -> "Tue"; case 3 -> "Wed"; default -> "?"; }; where arrow cases never fall through, several labels can share one arm, a block arm returns its value with yield, and the whole expression must be exhaustive.
What to remember
In this answer, we traced the colon-style switch and saw that it prints TueWed because execution falls through from case 2 into case 3 until the break. We rewrote it as a switch expression with arrow cases, which never fall through, group labels with commas, give a value from a block with yield, and must be exhaustive, which makes leaving out default over enums and sealed types a safety feature. We also saw that a classic switch on null throws NullPointerException, and that Java 21 adds case null.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It prints TueWed. With colon-style cases, execution jumps to the matching case and then falls through into every following case until it meets a break, so case 2 prints Tue and carries on into case 3, which prints Wed and breaks. As a switch expression we write String name = switch (day) { case 1 -> "Mon"; case 2 -> "Tue"; case 3 -> "Wed"; default -> "?"; }; where arrow cases never fall through, several labels can share one arm, a block arm returns its value with yield, and the whole expression must be exhaustive.
Why this question is asked
Fall-through is one of the oldest traps in Java, inherited from C, and it still causes real bugs: a missing break silently runs the next case, and the code looks fine at a glance. Interviewers show this snippet because it checks two things at once: whether we read the old switch statement precisely, and whether we know the modern switch expression that removes the trap. In this answer, we will trace the old switch step by step, see why it behaves that way, rewrite it as a switch expression, look at multiple labels, yield and exhaustiveness, and finish with what happens when the value is null.
1Tracing the Original Switch
A switch statement with colon-style cases works like a list of labels inside one block of code. Java evaluates the expression, here day, and jumps to the label whose value matches. From that point it simply runs the statements in order. A case label is only a place to jump to; it does not end anything. The only things that leave the switch are break, return, throw, or reaching its closing brace.
So with day equal to 2, execution jumps to case 2 and prints Tue. There is no break, so it continues to the next line, which belongs to case 3, and prints Wed. Then it meets break and leaves. The default label is never reached. The output is TueWed, on one line, because the code uses print rather than println.
- Evaluate day2
- Jump to the label case 2
- Run print("Tue")
- Is there a break?YesLeave the switchNoFall into case 3
- Run print("Wed")
- break: leave the switchoutput
TueWed
6 steps in order. Play them, or step through them one at a time.
A case label is only a jump target; without break, execution keeps going.
For day 1 it would print MonTueWed, for day 3 just Wed, and for any other value a single question mark. Notice that only case 3 is correct on its own; cases 1 and 2 are bugs that a quick read misses.
2Rewriting It as a Switch Expression
Since Java 14, switch can also be an expression, something that produces a value, and it can use arrow-style cases. An arrow case runs only the code to the right of the arrow and then leaves. There is no fall-through at all, so there is no break to forget.
Notice what changed. The switch now gives a value, so we assign it once to name instead of printing in every branch. Each arm is a single expression, so there is nothing to fall into. And the semicolon after the closing brace is required, because the whole switch is part of an assignment statement.
3Several Labels, Blocks and yield
Arrow cases can list several labels separated by commas, which replaces the one place where fall-through was useful: grouping values that share a result. When an arm needs more than one statement, we write a block in braces and give the value with yield. yield is to a switch expression what return is to a method: it ends the arm and supplies the result.
We cannot use return inside a switch expression arm to give its value, because return would try to leave the whole method. That is why the language needed yield.
4Why a Switch Expression Must Be Exhaustive
A switch expression must produce a value for every possible input, so the compiler requires it to be exhaustive. For an int there are four billion values, so in practice we need a default. For an enum, covering every constant is enough, and for a sealed type, covering every permitted subtype is enough. In those two cases leaving out the default is actually better, because adding a new constant or subtype later makes the switch fail to compile until someone handles it.
A switch statement, the kind without a value, can also use arrows, and for an int it does not have to be exhaustive. So the arrow form alone removes fall-through, and the expression form adds the exhaustiveness check.
5What Happens When the Value Is null?
A switch on a String or an enum works on the object's content, so a null value cannot match anything. A classic switch, in either colon or arrow form, throws NullPointerException before choosing a case. For a String, the generated code calls hashCode() on the value, and for an enum it calls ordinal(), and both fail on null. Since Java 21, a switch can declare case null explicitly, and then null goes to that arm instead of throwing.
6Follow-up questions
What happens at run time if a switch on a String receives null?
It throws NullPointerException, even when there is a default arm, because the compiled code calls hashCode() on the value before it can choose a case. Since Java 21 we can add case null, or combine it as case null, default -> ..., to handle null inside the switch instead of checking for it before.
Is fall-through ever useful?
Occasionally, when several cases share a tail of work, but arrow cases with several labels cover the common grouping case. When we keep intentional fall-through in old code, we mark it with a comment so readers and tools such as Checkstyle know it is deliberate.
Can we mix arrow and colon cases in one switch?
No. A switch uses one style for all its cases. The colon style can also be used in a switch expression, with yield to give the value, but then fall-through applies again, so the arrow style is the one to prefer.
7Conclusion
In this answer, we traced the colon-style switch and saw that it prints TueWed because execution falls through from case 2 into case 3 until the break. We rewrote it as a switch expression with arrow cases, which never fall through, group labels with commas, give a value from a block with yield, and must be exhaustive, which makes leaving out default over enums and sealed types a safety feature. We also saw that a classic switch on null throws NullPointerException, and that Java 21 adds case null.
Open with
It prints TueWed. In a colon-style switch a case label is only a jump target, so for day 2 it jumps to case 2, prints Tue, and because there is no break it falls through into case 3, prints Wed, and then the break exits.
Then, if they want more
Default is never reached. I would rewrite it as a switch expression: String name = switch (day) { case 1 -> "Mon"; case 2 -> "Tue"; case 3 -> "Wed"; default -> "?"; }. Arrow cases never fall through, so there is no break to forget, and the switch gives a value I assign once.
I can group labels, like case 6, 7 -> "Weekend", and if an arm needs several statements I use a block and yield the value, because return would leave the method.
A switch expression has to be exhaustive, so for an int I need a default, while for an enum or a sealed type covering every case is enough, and leaving out default there is better because a new constant then breaks the build instead of being silently handled.
One more detail: a classic switch on a null String throws NullPointerException even with a default, unless I add case null, which is allowed since Java 21.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does this print when day is 2, and how would you rewrite it as a switch expression?": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/2b8374f1fe/