Back to Course Overview
Chapter 720 min read

Java switch Statement

Master multi-way branching and equality testing against constant values in Java.

Released1995
Core MottoClean Multi-Branch Decision Structures
CreatorsJames Gosling / Sun Microsystems

1. Java switch Statement Fundamentals

A switch statement allows a variable to be tested for equality against a list of values.
Used when multiple if-else statements become cumbersome, executing code blocks based on matched case values.
Java switch Statement Flow Diagram

Mechanics & Basic Syntax

Purpose:Tests a variable against several candidate values (cases) sequentially.
Case Labels:Each value inside a `case` clause acts as a branch target.
Break Keyword:Terminates the `switch` block once a match is executed, preventing control from falling through into subsequent cases.
Default Clause:An optional catch-all block that executes if none of the explicit case values match.
java
switch(expression) {
   case value1:
      // Statements
      break; // optional
   
   case value2:
      // Statements
      break; // optional
   
   // Any number of case statements
   default: // Optional
      // Statements
}

Core Rules for Using switch

Allowed Data Types:Variables tested in `switch` can be primitive integers or convertible types (`byte`, `short`, `char`, `int`), `String` objects, or `Enum` types.
Constant Expressions:Case values must be literal constants or compile-time constants matching the expression's data type.
Fall-Through Behavior:If a `case` omits the `break` statement, execution continues into the next `case` regardless of whether that case matches.

2. Branching by Primitive, String, and Integer Types

Java switch supports evaluating char primitives, integers, and String literals.

Example 1: Switching on char Data Types

In this example, the variable `grade` holds `'C'`. Notice how `case 'B'` and `case 'C'` stack together to share the same code block.
java
public class Test {
   public static void main(String args[]) {
      char grade = 'C';

      switch(grade) {
         case 'A' :
            System.out.println("Excellent!"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

Example 2: Switching on int and String Types

Integer and String comparisons follow identical rules. Below is a String-based evaluation matching `"C"` directly.
java
public class Test {
   public static void main(String args[]) {
      String grade = "C";

      switch(grade) {
         case "A" :
            System.out.println("Excellent!"); 
            break;
         case "B" :
         case "C" :
            System.out.println("Well done");
            break;
         case "D" :
            System.out.println("You passed");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

3. The default Clause Behavior

The default keyword specifies an execution fallback when no case value matches the expression.

With vs. Without the default Keyword

With `default`:When `month = 13`, none of the numerical cases match, triggering the fallback block to output `"Invalid month"`.
Without `default`:Omitting `default` means the program silently bypasses the entire `switch` block when no matches are found, producing no output.
java
public class SwitchWithDefault {
    public static void main(String[] args) {
        int month = 13; // Invalid month example

        switch (month) {
            case 1:
                System.out.println("January");
                break;
            case 2:
                System.out.println("February");
                break;
            case 12:
                System.out.println("December");
                break;
            default:
                System.out.println("Invalid month");
        }
    }
}
Classroom REMINDER
What happens if you intentionally omit the break statement between two case blocks in a Java switch statement?
Takeaway Goal: Omitting the break statement causes 'fall-through' behavior. Java will continue executing the code of all subsequent case blocks below the matched case until it hits a break statement or reaches the end of the switch block.