Using Switch Statement in Java

To run one of several blocks of code depending on the value of an expression in Java, use the switch statement. The switch compares the value with each case label and runs the code after the matching one. A break statement ends the switch, and the default label runs when no case matches. In this Java Switch Statement Example, we get a color name by its number with a switch statement. Click Execute to run the Java Switch Statement Example online and see the result.
Using Switch Statement in Java Execute
public class SwitchDemo {
    public static void main(String[] args) {
        int color = 3;
        String colorName;

        switch (color) {
            case 1:
                colorName = "Red";
                break;
            case 2:
                colorName = "Green";
                break;
            case 3:
                colorName = "Blue";
                break;
            default:
                colorName = "Invalid color";
                break;
        }

        System.out.println("The color is: " + colorName);
    }
}

Java switch Statement Syntax

The switch keyword takes an expression in parentheses. Each case label holds one value to compare with it, and the default label catches every other value.

Java switch Statement Syntax
switch (expression) {
    case value1:
        // runs when expression equals value1
        break;
    case value2:
        // runs when expression equals value2
        break;
    default:
        // runs when no case matches
}

How does the switch statement work in Java?

The switch statement evaluates the expression and runs the statements after the matching case label. A break statement ends the switch. Without break, execution falls through to the statements of the next case. The default section runs when no case label matches.

A switch works with byte, short, char and int values, their wrapper classes Byte, Short, Character and Integer, enum types and String.

Java Switch Statement Examples

The following are examples of the switch statement in Java. Run them in the online Java code runner to see which case matches.

Switch on an int value

The switch compares the int with each case label and stops at the first break.

Java switch int Example
public class SwitchIntExample {
    public static void main(String[] args) {
        int day = 3;
        String name;
        switch (day) {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            default:
                name = "Unknown day";
        }
        System.out.println(name);
    }
}

// output: Wednesday

Switch on a String

A String in a switch is compared with each case label as if by the equals() method, so the comparison is case-sensitive.

Java switch String Example
public class SwitchStringExample {
    public static void main(String[] args) {
        String size = "M";
        switch (size) {
            case "S":
                System.out.println("Small");
                break;
            case "M":
                System.out.println("Medium");
                break;
            case "L":
                System.out.println("Large");
                break;
            default:
                System.out.println("Unknown size");
        }
    }
}

// output: Medium

Several case labels for one block

To run the same code for several values, list their case labels one after another before the code. Here months 4, 6, 9 and 11 share one block.

Java switch Fall Through Example
public class SwitchFallThroughExample {
    public static void main(String[] args) {
        int month = 2;
        int days;
        switch (month) {
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                days = 28;
                break;
            default:
                days = 31;
        }
        System.out.println("Days: " + days);
    }
}

// output: Days: 28

Switch expression with case -> labels

A switch can also be used as an expression, for example on the right side of an assignment. A switch expression always evaluates to one value: the value to the right of the matching arrow. Arrow labels do not fall through, so no break is needed, and one case can list several values separated by commas. A switch expression must cover every possible value, so it usually ends with default.

Java switch Expression Example
public class SwitchExpressionExample {
    public static void main(String[] args) {
        String day = "SUN";
        String type = switch (day) {
            case "SAT", "SUN" -> "Weekend";
            default -> "Weekday";
        };
        System.out.println(type);
    }
}

// output: Weekend

See also

Updated: