Java switch Statement

In this tutorial, you will learn to use the switch statement in Java to control the flow of your program’s execution with the help of examples.

The switch statement allows us to execute a block of code among many alternatives.

The syntax of the switch statement in Java is:

switch (expression) {

  case value1:
    // code
    break;

  case value2:
    // code
    break;

  default:
    // default statements
  }

1. How does the switch-case statement work?

The expression is evaluated once and compared with the values of each case.

  • If expression matches value1, the code of case value1 is executed.
  • If there is no match, the code of the default case is executed.

Example: Java switch Statement

// Java Program to check the size
class Main {
  public static void main(String[] args) {

    int number = 44;
    String size;

    // switch statement to check size
    switch (number) {

      case 29:
        size = "Small";
        break;

      case 42:
        size = "Medium";
        break;

      // match the value of week
      case 44:
        size = "Large";
        break;

      case 48:
        size = "Extra Large";
        break;

      default:
        size = "Unknown";
        break;

    }
    System.out.println("Size: " + size);
  }
}

Output:

Size: Large

2. The break Keyword

When Java reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block.

Fall-Through: If you omit the break, execution will continue to the next case (fall-through).

int day = 2;
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;
}

3. The default Keyword

The default keyword specifies some code to run if there is no case match.

int day = 4;
switch (day) {
  case 6:
    System.out.println("Today is Saturday");
    break;
  case 7:
    System.out.println("Today is Sunday");
    break;
  default:
    System.out.println("Looking forward to the Weekend");
}

4. Enhanced Switch (Java 12+)

Java 12 introduced a new switch expression that is more concise and less error-prone. It uses the -> arrow syntax and doesn't require break statements.

int day = 3;
String dayName = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    case 4 -> "Thursday";
    case 5 -> "Friday";
    case 6 -> "Saturday";
    case 7 -> "Sunday";
    default -> "Invalid day";
};
System.out.println(dayName);

Benefits:

  • No Fall-Through: You don't need break.
  • Returns a Value: Can be used as an expression.
  • Concise: Less boilerplate code.

Key Takeaways

  • Efficiency: switch is cleaner than many if-else-if blocks when checking one variable.
  • Case: Defines a value to match.
  • Break: Exits the switch block.
  • Default: Runs if no cases match (like else).
  • Enhanced Switch: Use -> for cleaner code in modern Java.

Common Pitfalls

[!WARNING] Fall-Through: Forgetting break causes the code to "fall through" and execute the next case's code too.

[!WARNING] Supported Types: You can't switch on long, float, double, or boolean. Only byte, short, char, int, String, and enums are allowed.

Challenge

Challenge

Task:

Write a switch statement that checks the value of 'day' (use 3). If it is 3, print 'Wednesday'.