CodeMIRAI
0 XP

switch

2 min read 5 runnable examples Quiz included

Classic switch

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

switch compares one value with a list of case labels. break leaves the switch, and default runs when no case matches.

Fall-through

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

Without break, execution falls through into the following cases and keeps going, so this prints Tuesday, Wednesday and Another day. A forgotten break is the classic switch bug. Occasionally fall-through is deliberate, to share code between cases:

Java
case "Sat":
case "Sun":
    System.out.println("Weekend");
    break;

The arrow form

Since Java 14, case ... -> runs exactly one branch, with no fall-through and no break to forget, and a case can list several values:

Java
public class Main {
    public static void main(String[] args) {
        String day = "Sat";
        switch (day) {
            case "Mon", "Tue", "Wed", "Thu", "Fri" -> System.out.println("Weekday");
            case "Sat", "Sun" -> System.out.println("Weekend");
            default -> System.out.println("Not a day");
        }
    }
}

Switch expressions

A switch can also produce a value:

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

        String size = "L";
        double price = switch (size) {
            case "S" -> 2.50;
            case "M" -> 3.20;
            case "L" -> {
                double base = 3.20;
                yield base + 0.80;      // yield gives a block's value
            }
            default -> throw new IllegalArgumentException("Unknown size: " + size);
        };
        System.out.println("Price: " + price);
    }
}
  • A switch expression must cover every possible value, which is why these have a default.
  • A case that needs several statements uses a block, and yield hands back its value.
  • A case can also throw, for values that should never happen.

Switching on an enum

Java
public class Main {
    enum Level { BEGINNER, INTERMEDIATE, ADVANCED }

    public static void main(String[] args) {
        Level level = Level.INTERMEDIATE;
        String advice = switch (level) {
            case BEGINNER -> "Start with variables and loops.";
            case INTERMEDIATE -> "Try object-oriented design.";
            case ADVANCED -> "Explore streams and concurrency.";
        };
        System.out.println(advice);
    }
}

An enum is a type with a fixed set of named values. Here every value has a case, so no default is needed. Add a fourth level later, and the compiler points to every switch that doesn't handle it yet.

You can switch on int, char, String and enum values (and byte, short and their wrapper classes), but not on double, long or boolean.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.