5.4 switch Statements, Supported Types & Fall-Through Rules
Key Takeaways
Java SE 8 switch supports byte, short, char, int, their wrappers (Byte, Short, Character, Integer), String, and enum—never long, float, double, or boolean
Each case label uses an assignment-compatible constant expression or, for an enum selector, an unqualified constant of that enum. Duplicate case values are illegal.
Switching on a null String, wrapper, or enum throws NullPointerException immediately, even if a default branch is present
Execution begins at the matching label or the default when no case matches. It runs through later labels if preceding statements complete normally.
A default label can appear anywhere. When no case matches, execution starts there and can fall through to the statements below it.
The Java SE 8 switch statement replaces multi-branch if-else chains when comparing a single selector value against fixed constants. On the 1Z0-808 exam, switch questions test two distinct areas: compiler rules (which types and case expressions are legal) and execution flow (missing break statements and tricky default placement).
Supported vs. Unsupported switch Selector Types
In Java SE 8, the switch selector expression must have one of the supported types below:
| Category | Supported in Java SE 8 | Unsupported (Compile-Time Error) |
|---|---|---|
| Integral primitives | byte, short, char, int | long, float, double, boolean |
| Wrapper classes | Byte, Short, Character, Integer | Long, Float, Double, Boolean |
| Reference & special types | String or an enum type | Object, StringBuilder, array types |
Notice the clear pattern: long, float, double, and boolean (along with their wrappers) are never allowed in a Java SE 8 switch statement:
long id = 10L;
// switch (id) { case 10: break; } // DOES NOT COMPILE: incompatible types: possible lossy conversion from long to int
boolean ready = true;
// switch (ready) { case true: break; } // DOES NOT COMPILE: boolean cannot be switched on
The null Selector NullPointerException Trap
When the switch selector is a reference type (String, Integer, Byte, Short, Character, or an enum), Java must unbox the wrapper or inspect the object before checking any case or default labels. If the selector is null, the switch immediately throws NullPointerException at runtime—even if a default: branch exists:
Integer code = null;
switch (code) { // Throws NullPointerException here!
case 1: System.out.println("One"); break;
default: System.out.println("Default"); // Never reached
}
If null is an expected input, handle it explicitly before the switch, for example with if (code == null).
Case Labels and Enum Constants
A non-enum case uses an assignment-compatible constant expression. For a boxed selector such as Byte, the constant must fit the corresponding primitive when narrowed and boxed. An enum selector uses constants from its own enum.
void check(final int param, int input) {
final int VALID = 10; // Constant variable
int nonFinal = 20; // Mutable variable
final int DELAYED;
DELAYED = 30; // Blank final, not a constant variable
switch (input) {
case VALID: // COMPILES
break;
// case nonFinal: // DOES NOT COMPILE: constant expression required
// case DELAYED: // DOES NOT COMPILE: constant expression required
// case param: // DOES NOT COMPILE: method parameter value is runtime-only
// case null: // DOES NOT COMPILE in Java SE 8
}
}
Check four more label rules:
- Range checking on narrower types: When switching on
byte,short, orchar, everycaseconstant must fit inside that type's range:
byte b = 10;
switch (b) {
case 10: break;
// case 200: break; // DOES NOT COMPILE: 200 is outside byte range (-128..127)
}
- Duplicate
casevalues: Twocaselabels cannot resolve to the same constant—includingcase 65:andcase 'A':in anintorcharswitch (because'A'is65). enumcasesyntax: Inside anenumswitch, write the unqualified constant name (case MONDAY:), never the qualified name (case Day.MONDAY:-> DOES NOT COMPILE!).Stringmatching is case-sensitive:case "YES":does not match"yes".
enum Day { MON, TUE, WED }
Day today = Day.TUE;
switch (today) {
case MON:
System.out.println("Start");
break;
// case Day.TUE: // DOES NOT COMPILE: an enum switch case label must be the unqualified name
case TUE:
System.out.println("Midweek");
break;
}
Likewise, when switching on a String, switch compares values using String.equals() semantics (after checking for null), so "Mon" and "MON" are distinct cases:
String cmd = "start";
switch (cmd) {
case "START":
System.out.println("Upper");
break;
case "start":
System.out.println("Lower"); // Matches here
break;
}
break vs. Fall-Through Execution
Once Java finds a matching case label, it jumps to that entry point and executes statements sequentially downward—ignoring all subsequent case labels—until it hits a break, return, throw, or the closing } of the switch block. That downward flow across case labels is called fall-through.
Stack multiple case labels when different values share the same statements. This simplified calendar example uses 28 for February in a common year:
int month = 2;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
System.out.println(31);
break;
case 4: case 6: case 9: case 11:
System.out.println(30);
break;
case 2:
System.out.println(28);
break;
}
When break is omitted accidentally, execution keeps running into the next cases until a break is reached:
int level = 2;
switch (level) {
case 1: System.out.print("1");
case 2: System.out.print("2");
case 3: System.out.print("3"); break;
case 4: System.out.print("4");
}
// Output: 23
default Placement & Fall-Through From default
Unlike else in an if-else chain, the default: label does not have to be last. A switch block can have at most one default: label. You can place it before, between, or after the case labels. Remember two ironclad rules:
- A matching case sets the entry point. Even if
default:is written first, control starts at a matching case. Execution can still reachdefault:by falling through from an earlier label. - If
default:runs without abreak, it falls through downward into anycasestatements written below it!
Trace both inputs on this classic exam snippet:
void runSwitch(int day) {
switch (day) {
default:
System.out.print("D");
case 1:
System.out.print("1");
break;
case 2:
System.out.print("2");
}
}
runSwitch(1):case 1matches directly, skipsdefault, prints1, hitsbreak. Output:1.runSwitch(9): Nocasematches, jumps todefault, printsD, has nobreak, falls straight intocase 1, prints1, then hitsbreak. Output:D1!
Note
Case labels do not introduce nested scopes. A local declared at switch-block level can remain in scope under subsequent labels, where it may fail definite assignment. Declaring int x = 1; under case 1: and int x = 2; under case 2: fails to compile (variable x is already defined) unless each case wraps its declaration inside { ... } braces.
What is the output of the following code?
int x = 5;
switch (x) {
default:
System.out.print("D");
case 1:
System.out.print("1");
case 2:
System.out.print("2");
break;
case 3:
System.out.print("3");
}
D
12
D123
D12
In the shown method context, which snippet compiles in Java SE 8? Options without a method declaration are statements inside a method.
long v = 1L; switch (v) { case 1: break; }
final int c = 5; int x = 5; switch (x) { case c: break; }
void test(final int p) { switch (1) { case p: break; } }
byte b = 1; switch (b) { case 150: break; }
What happens when the following code runs?
String mode = null;
switch (mode) {
case "FAST":
System.out.println("Fast");
break;
default:
System.out.println("Standard");
}
Prints Fast
Prints Standard
Throws NullPointerException
Does not compile
Sections you finish are checked off in the contents.