Java Programming Handbook

    Java switch Statement

    The switch statement in Java is used when we need to test a variable against multiple possible values. It is an alternative to using multiple if-else-if statements and makes the code more readable and efficient.

    In this blog, we will cover:

    • What is the switch statement?
    • How does the switch statement work?
    • Syntax of switch
    • Example programs with explanations
    • Rules and limitations of switch
    • Enhanced switch statement in Java 12+

    What is the switch Statement?#

    A switch statement allows a variable to be tested against multiple values, executing different blocks of code depending on which value matches.

    Instead of writing multiple if-else-if conditions, we can use switch for better readability.

    How Does the switch Statement Work?#

    • The switch statement evaluates an expression.
    • The result of the expression is compared with multiple case values.
    • If a match is found, the corresponding block of code executes.
    • The break statement stops execution after a case is matched.
    • If no cases match, the default case (if provided) is executed.

    Syntax of switch Statement#

    switch (expression) { case value1: // Code to execute if expression == value1 break; case value2: // Code to execute if expression == value2 break; default: // Code to execute if no case matches }

    Example 1: Basic switch Statement#

    class Main { public static void main(String[] args) { int day = 3; // 1 - Monday, 2 - Tuesday, ..., 7 - Sunday 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; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid day"); } } }

    Expected Output#

    Wednesday

    Explanation:

    • The variable day is 3, so it matches case 3:.
    • "Wednesday" is printed.
    • The break statement exits the switch block.

    Example 2: switch with char#

    class Main { public static void main(String[] args) { char grade = 'B'; switch (grade) { case 'A': System.out.println("Excellent!"); break; case 'B': System.out.println("Good job!"); break; case 'C': System.out.println("You passed."); break; case 'D': System.out.println("Better luck next time."); break; default: System.out.println("Invalid grade"); } } }

    Expected Output#

    Good job!

    Explanation:

    • The variable grade is 'B', so it matches case 'B':.
    • "Good job!" is printed.

    Example 3: switch without break#

    If we do not use the break statement, execution will continue to the next case.

    class Main { public static void main(String[] args) { int num = 2; switch (num) { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Invalid number"); } } }

    Expected Output#

    Two Three Invalid number

    Explanation:

    • The value 2 matches case 2:.
    • Since there's no break, execution continues to the next cases.

    Example 4: switch with Strings (Java 7+)#

    Java allows switch statements with String values starting from Java 7.

    class Main { public static void main(String[] args) { String fruit = "Apple"; switch (fruit) { case "Apple": System.out.println("Apples are red or green."); break; case "Banana": System.out.println("Bananas are yellow."); break; case "Orange": System.out.println("Oranges are orange."); break; default: System.out.println("Unknown fruit"); } } }

    Expected Output#

    Apples are red or green.

    Example 5: Nested switch Statement#

    We can use one switch inside another switch.

    class Main { public static void main(String[] args) { int country = 1; // 1 - India, 2 - USA int state = 2; // 1 - Maharashtra, 2 - Karnataka switch (country) { case 1: System.out.println("Country: India"); switch (state) { case 1: System.out.println("State: Maharashtra"); break; case 2: System.out.println("State: Karnataka"); break; default: System.out.println("Unknown state"); } break; case 2: System.out.println("Country: USA"); break; default: System.out.println("Unknown country"); } } }

    Expected Output#

    Country: India State: Karnataka

    Enhanced switch in Java 12+#

    From Java 12 onwards, switch is enhanced with a new syntax.

    Example 6: Enhanced switch with >#

    class Main { public static void main(String[] args) { int day = 4; String result = switch (day) { case 1, 2, 3 -> "Weekday"; case 4, 5 -> "Almost Weekend"; case 6, 7 -> "Weekend"; default -> "Invalid day"; }; System.out.println(result); } }

    Expected Output#

    Almost Weekend

    Explanation:

    • The switch returns a value directly.
    • We use > instead of : to make the code cleaner.

    Rules and Limitations of switch#

    • Expression type: switch works with byte, short, char, int, String, and enum.
    • No long, float, double, or boolean values allowed.
    • Duplicate case values are not allowed.
    • Use break to prevent fall-through.
    • default is optional but recommended.

    Conclusion#

    In this blog, we have learned:

    • The switch statement is an alternative to multiple if-else-if conditions.
    • It compares a variable against multiple case values.
    • The break statement prevents execution from falling through to the next case.
    • The default case executes when no other case matches.
    • switch supports String (Java 7+) and has an enhanced form (Java 12+).

    Using switch makes your code cleaner and more readable, especially when checking a single variable against multiple possible values.

    Last updated on Apr 09, 2025