The switch statement
A switch chooses one branch from a list of constants. It is older than the rest of the language around it, and its two habits are the ones to learn first: a branch ends only when a break says so, and a selector may be a number, a char, an enum or a String, but not a boolean, a long or a floating-point value.
The switch statement
The selector is one expression, each case is one constant of its type, and the block runs from the matching label to the first break or to the end of the switch. The program below switches on an int and has a break in every branch.
The file Days.java:
public class Days {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("another day");
}
System.out.println("after the switch");
}
}
javac -Xlint:all Days.java
java Days
Wednesday
after the switch
The value 3 matches the third label, the branch prints one line and the break leaves the switch. The statement after the switch runs in either case, because a switch is one statement of the method and not a method of its own.
break
break ends the nearest enclosing switch, loop or labelled block. Without it the program keeps running the statements of the following labels, which is called fall-through. It is legal, and it is occasionally what a program wants: several values that share the same code can list their labels with no statements between them, and the shared code sits under the last label.
The compiler does not stop a fall-through, but with -Xlint:all it warns about it, because the usual cause is a forgotten break. The next program falls through on purpose and prints the warning that comes with it.
Fall-through
public class FallThrough {
public static void main(String[] args) {
for (int day = 1; day <= 3; day++) {
System.out.print("day " + day + ": ");
switch (day) {
case 1:
System.out.print("Mon ");
case 2:
System.out.print("Tue ");
case 3:
System.out.print("Wed ");
break;
default:
System.out.print("none");
}
System.out.println();
}
}
}
javac -Xlint:all FallThrough.java
java FallThrough
FallThrough.java:8: warning: [fallthrough] possible fall-through into case
case 2:
^
FallThrough.java:10: warning: [fallthrough] possible fall-through into case
case 3:
^
2 warnings
day 1: Mon Tue Wed
day 2: Tue Wed
day 3: Wed
Day 1 enters at case 1 and runs every statement to the break, so it prints three names; day 2 starts at its own label. The two warnings point at the labels that are reached by falling through, not at the missing break, which is the line a reader would look for first.
default and a String selector
default is the label that matches when no case does. It may stand anywhere in the switch, and only its position in the fall-through order depends on where it is written. Since Java 7 the selector may also be a String, compared with equals rather than with ==.
public class Menu {
public static void main(String[] args) {
String command = "stop";
switch (command) {
case "start":
System.out.println("starting");
break;
case "stop":
System.out.println("stopping");
break;
default:
System.out.println("unknown: " + command);
}
}
}
javac -Xlint:all Menu.java
java Menu
stopping
A null selector throws NullPointerException when the switch tries to match it, so a command that may be missing is checked before the switch. The switch page has the same statement with more branches, and the next lesson replaces the whole statement with an expression that cannot fall through.
Exercises
- Add a break to every branch of FallThrough.java and run it again. What does each day print now?
- Move default to the top of the switch in Days.java and change the day to 5. Which branch runs, and does the position matter?
- Write a switch on a char that prints vowel for a, e, i, o and u and consonant otherwise.
- Try a switch whose selector is a long. What does javac say?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which statement ends a switch branch?
Answer
break
break leaves the switch; without it the next labels run too.
-
What happens when a matching branch has no break?
Answer
The statements of the next labels run as well
Execution falls through into the following case labels until a break or the end of the switch.
-
Which type cannot be a switch selector?
Answer
boolean
A switch matches constants, and a boolean has only two values that an if already handles; long and floating-point types are refused too.
-
What does this program print?
public class Q1 { public static void main(String[] args) { int day = 2; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; default: System.out.println("another day"); } } }Answer
Tuesday
The selector is 2, so the second branch runs and the break ends the switch.
-
What does this program print?
public class Q2 { public static void main(String[] args) { int n = 1; switch (n) { case 1: System.out.println("one"); case 2: System.out.println("two"); break; default: System.out.println("other"); } } }Answer
one / two
The first branch has no break, so the second branch runs too and its break ends the switch.
-
What is the warning group for a fall-through?
Answer
[fallthrough]
-Xlint:fallthrough warns about a case label reached by falling through from the one before it.
-
What does this program print?
public class Q3 { public static void main(String[] args) { String command = "start"; switch (command) { case "start": System.out.println("starting"); break; case "stop": System.out.println("stopping"); break; default: System.out.println("unknown"); } } }Answer
starting
A String selector matches its case labels with equals, and start matches the first one.
-
What does this program print?
public class Q4 { public static void main(String[] args) { int n = 9; switch (n) { case 1: System.out.println("one"); break; default: System.out.println("default"); break; case 2: System.out.println("two"); } } }Answer
default
No case matches 9, so default runs and its break stops before the case 2 statements.
-
What does a
caselabel have to be?Answer
A constant the compiler knows at compile time
Case labels are compile-time constants; the selector is the only value read when the switch runs.
-
What does this program print?
public class Q5 { public static void main(String[] args) { char letter = 'e'; switch (letter) { case 'a': case 'e': case 'i': System.out.println("vowel"); break; default: System.out.println("consonant"); } } }Answer
vowel
The empty labels a and i fall through to the shared vowel statement, and e matches one of them.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev