Java Switch Statement

switch selects one of many blocks to run, based on the value of an expression. It works with int, char, String, enums and wrapper types.

Java
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("Other day");
}
Wednesday
Fall-throughWithout break, execution continues into the next case.

Switch Expression (Java 14+)

Java
String d = "SUN";
String type = switch (d) {
    case "SAT", "SUN" -> "Weekend";
    default -> "Weekday";
};
System.out.println(type);
Weekend
Track your progress through the Java course