Java switch Statement
Use the switch statement in Java to branch on values, with case labels, break, default, and fall-through behavior.
When you need to compare a single value against many possibilities, a long if/else if chain quickly becomes noisy. The switch statement is Java's compact alternative — read the value once, jump to the matching case, run its block.
Basic syntax
switch (value) {
case label1:
// body
break;
case label2:
// body
break;
default:
// body
break;
}A small example:
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("Other");
break;
}The switch jumps to case 3:, prints Wednesday, hits break, and exits.
The break is essential
Without break, execution falls through to the next case — even if its label doesn't match. This is a deliberate feature of the C-style switch, but it's the source of countless bugs in Java code:
switch (day) {
case 1:
System.out.println("Monday");
// no break!
case 2:
System.out.println("Tuesday");
break;
}When day == 1, this prints both Monday and Tuesday. Always add break unless you intentionally want fall-through.
Intentional fall-through
Sometimes fall-through is exactly what you want — grouping multiple labels under the same block:
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("Weekday");
break;
case 6:
case 7:
System.out.println("Weekend");
break;
}When you do this on purpose, add a // fall through comment so reviewers don't think you forgot a break. (The new switch expression syntax — covered in the next chapter — fixes this footgun entirely.)
What can a switch value be?
A traditional switch accepts:
- All integer types:
byte,short,int,char - Their boxed wrappers:
Byte,Short,Integer,Character String(since Java 7)enumconstants
Not allowed: long, float, double, boolean, or arbitrary objects. For those, use if/else.
String role = "admin";
switch (role) {
case "admin":
System.out.println("Full access");
break;
case "editor":
System.out.println("Write access");
break;
case "viewer":
System.out.println("Read access");
break;
default:
System.out.println("No access");
break;
}String comparison in a switch uses String.equals semantics — case-sensitive.
default — the catch-all
default runs when no case matches. It's not required, but including one is good practice; it makes the behavior for unexpected values explicit.
default doesn't have to be last. By convention it goes at the bottom, but the compiler accepts it anywhere — execution falls through it just like any other case if you forget break.
Switching on enum
enum and switch are a natural pair. Inside a switch on an enum value, you don't need to qualify the constant name:
enum Status { PENDING, ACTIVE, DONE }
Status s = Status.ACTIVE;
switch (s) {
case PENDING: // not Status.PENDING
System.out.println("Waiting...");
break;
case ACTIVE:
System.out.println("In progress");
break;
case DONE:
System.out.println("Finished");
break;
}A worked example
What's next
Java 14 introduced switch expressions, which return a value, eliminate fall-through, and support multi-label cases — modern Java code prefers them whenever you're targeting Java 14 or newer.
Practice
In a traditional Java switch, what happens if you omit break in a case block?