Start Coding Now

Switch Case in Java - Syntax and Examples

Master the switch statement in Java. Learn about break, default, and new switch expressions in Java 14+.

Switch Statement

Selects one of many code blocks to be executed.

int day = 4;
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("Weekend");
}

Important Points

  • The break keyword stops execution. If omitted, it "falls through" to the next case.
  • The default case runs if no match is found.
  • Supports primitives (byte, short, int, char), Enums, and String.

Frequently Asked Questions