TExcelTAD
Home › Java › Java Switch

Java Switch

When one value can lead to many different branches, a long if...else if chain gets hard to read. A switch selects a branch by comparing a value against a list of case labels. Modern Java (14+) has a much nicer form of switch, and you'll see both styles in real code.

Arrow-style switch (modern)

Each case uses -> and runs exactly one statement or block. There is no fall-through, so no break is needed:

java · try it yourself
class Main {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1 -> System.out.println("Monday");
            case 2 -> System.out.println("Tuesday");
            case 3 -> System.out.println("Wednesday");
            case 4 -> System.out.println("Thursday");
            case 5 -> System.out.println("Friday");
            case 6, 7 -> System.out.println("Weekend!");
            default -> System.out.println("Invalid day");
        }
    }
}
Wednesday

case 6, 7 handles several values in one label. default runs when no case matches.

Switch expressions

A switch can also produce a value. This is called a switch expression and is often the cleanest way to map one value to another:

java · try it yourself
class Main {
    public static void main(String[] args) {
        String[] codes = {"GB", "FR", "JP", "XX"};
        for (String code : codes) {
            String currency = switch (code) {
                case "GB" -> "Pound";
                case "FR", "DE", "IT" -> "Euro";
                case "JP" -> "Yen";
                default -> "Unknown";
            };
            System.out.println(code + ": " + currency);
        }
    }
}
GB: Pound
FR: Euro
JP: Yen
XX: Unknown

A switch expression must be exhaustive — every possible value must be covered, which usually means you need a default. If a branch needs several statements, use a block and yield the value:

java · try it yourself
class Main {
    public static void main(String[] args) {
        int month = 2;
        int year = 2028;
        int days = switch (month) {
            case 4, 6, 9, 11 -> 30;
            case 2 -> {
                boolean leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
                yield leap ? 29 : 28;
            }
            default -> 31;
        };
        System.out.println("Days: " + days);
    }
}
Days: 29

Classic switch with break

The original syntax uses case value: (with a colon). Execution jumps to the matching label and continues downwards until a break — even into the following cases. This is called fall-through:

java · try it yourself
class Main {
    public static void main(String[] args) {
        int level = 2;
        switch (level) {
            case 1:
                System.out.println("Unlocked: sword");
                break;
            case 2:
                System.out.println("Unlocked: shield");
                // no break: falls through!
            case 3:
                System.out.println("Unlocked: bow");
                break;
            default:
                System.out.println("Nothing unlocked");
        }
    }
}
Unlocked: shield
Unlocked: bow

Forgetting break is a common bug, which is exactly why the arrow form was added. Prefer -> in new code.

What can you switch on?

Allowed types Example
int, short, byte, char (and their wrappers) switch (count)
String switch (command)
enums switch (color)
any object, with pattern matching (Java 21+) case Integer i ->

long, double, float and boolean can't be used with classic constant labels. Pattern matching in switch is covered in the Modern Java chapter.

Classic case x: Arrow case x ->
Fall-through yes, until break never
Several values stack labels case 1, 2 ->
Can return a value no (statement only) yes, as an expression

Test yourself

In a classic switch, what happens if a matching case has no break?

  1. Compile error
  2. Execution continues into the next case
  3. The switch restarts
  4. The default branch runs only

Which keyword returns a value from a block inside a switch expression?

  1. return
  2. break
  3. yield
  4. give

What prints? String s = switch (5) { case 1, 2 -> "low"; default -> "high"; }; System.out.println(s);

  1. low
  2. high
  3. nothing
  4. Compile error