Switch statement in Java - Time & Space Complexity
We want to see how the time it takes to run a switch statement changes as the input changes.
Specifically, we ask: How does the time grow when we add more cases?
Analyze the time complexity of the following code snippet.
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;
case 4: System.out.println("Thursday"); break;
default: System.out.println("Other day");
}
This code prints the name of the day based on the number given.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Table lookup or direct jump to the matching case.
- How many times: Constant time (O(1)), typically a single array access or hash lookup.
In Java, the compiler optimizes switch statements on primitives (like int) using a jump table (dense cases) or partial table/hashes (sparse), keeping execution constant.
| Input Size (number of cases) | Approx. Operations |
|---|---|
| 5 | 1 lookup |
| 50 | 1 lookup |
| 500 | 1 lookup |
Pattern observation: The number of operations stays constant regardless of cases.
Time Complexity: O(1)
This means constant time to execute, independent of case count, due to compiler optimizations like jump tables.
[X] Wrong: "A switch statement checks cases one by one like if-else, so O(n)."
[OK] Correct: Java compiles switches to efficient table-driven code (jump tables for dense ints), not linear scans.
Knowing switch optimizations shows deep JVM/compiler knowledge and ability to distinguish high-level code from low-level execution.
"What if cases are very sparse (e.g., 1, 1000, 10000)? How does Java handle it? (Hint: if-else chain or hashed switch)"
Practice
switch statement in Java?Solution
Step 1: Understand the role of switch
Theswitchstatement chooses one path from many based on a value.Step 2: Compare with other options
Loops repeat code, classes define objects, and exceptions handle errors, so they don't match.Final Answer:
To select one action from many based on a value -> Option BQuick Check:
Switch = Select action based on value [OK]
- Confusing switch with loops
- Thinking switch handles errors
- Mixing switch with class definitions
Solution
Step 1: Recall switch syntax
The correct syntax uses parentheses around the variable:switch (variable) { ... }.Step 2: Check other options
Options A, B, and D miss parentheses or use wrong brackets, causing syntax errors.Final Answer:
switch (variable) { ... } -> Option AQuick Check:
Switch needs parentheses around variable [OK]
- Omitting parentheses
- Using square brackets instead of parentheses
- Adding parentheses after variable name
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("Invalid day");
}Solution
Step 1: Identify the value of day
The variabledayis set to 3.Step 2: Match the case in switch
Case 3 matches, so it prints "Wednesday" and then breaks out of the switch.Final Answer:
Wednesday -> Option AQuick Check:
day=3 matches case 3 = Wednesday [OK]
- Forgetting break causes fall-through
- Choosing default when case matches
- Mixing case numbers and output strings
int num = 2;
switch (num) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
break;
default:
System.out.println("Other");
}Solution
Step 1: Check breaks in cases
Case 1 lacks abreak, so execution falls through to case 2.Step 2: Understand effect of missing break
Without break, both "One" and "Two" print when num=1, which may be unintended.Final Answer:
Missing break after case 1 -> Option DQuick Check:
Each case needs break to stop fall-through [OK]
- Forgetting break causes multiple prints
- Thinking default is mandatory
- Believing switch only works with strings
Solution
Step 1: Understand Java switch syntax for multiple cases
Java allows multiple case labels by stacking them without commas, likecase 1: case 2:.Step 2: Check each option's syntax
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; default: System.out.println("Invalid"); } correctly stacks cases. switch(day) { case 1,2,3,4,5: System.out.println("Weekday"); break; case 6,7: System.out.println("Weekend"); break; default: System.out.println("Invalid"); } uses commas (invalid), C uses || (invalid), D uses parentheses (invalid).Final Answer:
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; default: System.out.println("Invalid"); } -> Option CQuick Check:
Stack multiple cases by repeating case lines [OK]
- Using commas between case labels
- Using logical OR (||) in case labels
- Using parentheses around case labels
Ready to prove it?
Pass the test and this pattern moves unknown
