Switch expressions

Switch expressions — allow you to return values directly from the switch block, making it more concise.

int result = switch (expression) {
    case value1 -> result1;
    case value2 -> result2;
    default -> defaultResult;
};
int day = 3;
String dayName = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "Invalid day";
};
System.out.println(dayName);

Switch expressions — Structure map

Clickable & Draggable!

Switch expressions — Related pages: