switch-case

Multi-way branching

Interview Relevant: Switch expressions and pattern matching are hot topics
7 min read

Switch Statement in Java

The switch statement provides an elegant way to dispatch execution to different parts of code based on the value of an expression.

Supported Types

  • byte, short, char, int (primitives)
  • Byte, Short, Character, Integer (wrapper classes)
  • String (Java 7+)
  • enum types

⚠️ Important: long, float, double, and boolean are NOT allowed in switch!

Fall-Through Behavior

Without break, execution "falls through" to the next case. This can be intentional or a bug!

Switch Expressions (Java 14+)

Modern Java introduced switch expressions with arrow syntax -> that don't require break and can return values.

💡 Modern Java: Prefer switch expressions over traditional switch statements. They're more concise and less error-prone (no fall-through bugs).

Code Examples

Traditional switch statement with break

java
1// Traditional switch statement
2int day = 3;
3String dayName;
4
5switch (day) {
6    case 1:
7        dayName = "Monday";
8        break;
9    case 2:
10        dayName = "Tuesday";
11        break;
12    case 3:
13        dayName = "Wednesday";
14        break;
15    case 4:
16        dayName = "Thursday";
17        break;
18    case 5:
19        dayName = "Friday";
20        break;
21    default:
22        dayName = "Weekend";
23        break;
24}
25System.out.println(dayName);  // Wednesday

Switch with String and intentional fall-through

java
1// Switch with String (Java 7+)
2String command = "start";
3
4switch (command.toLowerCase()) {
5    case "start":
6        System.out.println("Starting...");
7        break;
8    case "stop":
9        System.out.println("Stopping...");
10        break;
11    case "pause":
12        System.out.println("Pausing...");
13        break;
14    default:
15        System.out.println("Unknown command");
16}
17
18// Fall-through example (intentional)
19int month = 2;
20int days;
21switch (month) {
22    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
23        days = 31;
24        break;
25    case 4: case 6: case 9: case 11:
26        days = 30;
27        break;
28    case 2:
29        days = 28;  // Simplified, ignoring leap year
30        break;
31    default:
32        days = 0;
33}

Modern switch expressions in Java 14+

java
1// Switch Expression (Java 14+) - Arrow syntax
2int day = 3;
3String dayType = switch (day) {
4    case 1, 2, 3, 4, 5 -> "Weekday";
5    case 6, 7 -> "Weekend";
6    default -> "Invalid";
7};
8
9System.out.println(dayType);  // Weekday
10
11// Switch expression with yield (for complex logic)
12String result = switch (day) {
13    case 1, 2, 3, 4, 5 -> {
14        System.out.println("Working day");
15        yield "Weekday";
16    }
17    case 6, 7 -> {
18        System.out.println("Rest day");
19        yield "Weekend";
20    }
21    default -> "Invalid";
22};

Switch with enum types - exhaustiveness checked

java
1// Switch with enum
2enum Status { PENDING, APPROVED, REJECTED, CANCELLED }
3
4Status status = Status.APPROVED;
5
6String message = switch (status) {
7    case PENDING -> "Your request is being processed";
8    case APPROVED -> "Congratulations! Approved!";
9    case REJECTED -> "Sorry, your request was rejected";
10    case CANCELLED -> "Request was cancelled";
11};
12
13// No default needed if all enum values handled!
14System.out.println(message);

Use Cases

  • Menu-driven programs
  • State machine implementation
  • Command pattern dispatch
  • Enum-based logic
  • Parsing command-line arguments
  • Calculator operations

Common Mistakes to Avoid

  • Forgetting break statements (unintentional fall-through)
  • Using unsupported types (long, float, double, boolean)
  • Not handling all enum cases
  • Duplicate case values
  • Using non-constant expressions in case labels
  • Not including default case for non-enum switches