Classic switch statement in Java
case labels, fall-through without break, default.
A jump table
A switch looks at one value and **jumps straight to the matching case label**. It's a tidy replacement for a long chain of if (day == 1) ... else if (day == 2) ....
switch (day) {
case 1:
System.out.println("Mon");
break;
case 2:
System.out.println("Tue");
break;
}Labels are doors, not walls
With classic colon labels (case 1:), a label is only an entry point. After jumping in, Java keeps running every following line, straight through other case labels, until it hits a break or the end of the switch. This is called fall-through.
case 1:
System.out.println("Easy");
// no break: keeps going!
case 2:
System.out.println("Hard");
break;Your turn
n is 1. What prints?
int n = 1;
switch (n) {
case 1: System.out.println("one");
case 2: System.out.println("two");
break;
case 3: System.out.println("three");
}oneone twoone two threetwo
Show the answer
one then two. Java jumps to case 1, falls through into case 2 (no break), and finally stops at the break. three never runs.
default and shared labels
default runs when no case matches. It's optional and may appear anywhere, not just last, and it falls through like any other label. Several labels can share one body: stack them (case 6: case 7:) or list them (case 6, 7:). Fall-through on purpose!
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}default in the middle
c is 9, which matches no case. What prints?
int c = 9;
switch (c) {
case 1: System.out.println("low");
break;
default: System.out.println("unknown");
case 2: System.out.println("two");
case 3: System.out.println("three");
break;
}unknownunknown two threeunknown twolow unknown
Show the answer
Nothing matches 9, so execution starts at default. default isn't special about fall-through: it continues into case 2 and case 3 and stops at that break.
Labels must be constant and unique
Every case label must be a compile-time constant (like 1, 'A' or a final constant), and no two labels may be equal. A repeated case 1 is a compile error: duplicate case label.
switch (x) {
case 1: System.out.println("a"); break;
case 1: System.out.println("b"); break;
} // compile error: duplicate case labelIn real projects
Accidental fall-through is such a common bug that javac -Xlint:fallthrough warns about it, and teams mark intentional fall-through with a // fall through comment. Most modern Java code avoids the problem entirely with arrow labels, coming up next.
Key takeaways
- Forgetting break makes execution fall through into the next case
- default is optional and may appear anywhere, not just last
- Case labels must be constants and unique
- Several labels can share one body: case 6: case 7: or case 6, 7:
💡 Old switch is like a slide with entry doors: you enter at the matching door and slide all the way down unless a break catches you.
In 1983 Tom Duff used C's switch fall-through to jump into the *middle* of a loop to speed up copying. "Duff's device" is perfectly legal C, and famously horrifying to read.
Practice questions
What does this print?
int n = 2;
switch (n) {
case 1: System.out.println("one");
case 2: System.out.println("two");
case 3: System.out.println("three");
default: System.out.println("other");
}- two
- two three
- two three other
- one two three other
Check your answer
two three other. Execution jumps to case 2, then falls through every later label because there's no break, including default.
What does this print?
int x = 1;
switch (x) {
case 1: System.out.println("a"); break;
case 1: System.out.println("b"); break;
}- a
- a b
- Compile error
- b
Check your answer
Compile error. Case labels must be unique. The compiler rejects the second case 1 with duplicate case label.