Sub topic 2
Switching on Enums
An enum is a type with a fixed list of named values, so a common job is to do something different for each value. Java's switch handles that job. This chapter shows the classic switch statement, the newer arrow style, and the switch expression, which can check that you handled every constant.
The Classic Switch Statement
A switch statement is a statement that picks a block of code to run based on a value. Each choice is a case label. With enums, you write the bare constant name in each label, such as SATURDAY, not Day.SATURDAY.
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public class Main {
static void describe(Day day) {
switch (day) {
case SATURDAY:
case SUNDAY:
System.out.println(day + " is a rest day");
break;
default:
System.out.println(day + " is a work day");
}
}
public static void main(String[] args) {
describe(Day.MONDAY); // MONDAY is a work day
describe(Day.SUNDAY); // SUNDAY is a rest day
}
}The break keyword ends the switch. If you forget it, Java keeps running into the next case. This is called fall-through, and it causes many bugs.
The Arrow Style
Since Java 14, you can write case X -> instead of case X:. Only the code after the arrow runs, so you never need break. You can also list several constants in one case, separated by commas.
switch (day) {
case SATURDAY, SUNDAY -> System.out.println("Rest");
default -> System.out.println("Work");
}Switch Expressions
A switch expression is a switch that produces a value. You can return it or store it in a variable. Each arrow gives the result for its case.
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public class Main {
static boolean isWeekend(Day day) {
return switch (day) {
case SATURDAY, SUNDAY -> true;
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> false;
};
}
public static void main(String[] args) {
System.out.println(isWeekend(Day.FRIDAY)); // false
System.out.println(isWeekend(Day.SATURDAY)); // true
}
}Note the semicolon after the closing brace. The whole switch is one expression inside a return statement.
Exhaustiveness: The Compiler Checks Your Work
A switch expression must be exhaustive, which means it has a result for every possible value. Here every constant of Day appears in a case, so the compiler accepts it without a default branch.
This matters when the enum changes. Suppose you later add HOLIDAY to Day. The switch expression above stops compiling, and the error points at the exact place you need to update. If you had written default -> false, the code would still compile, and HOLIDAY would quietly count as a weekday.
Statement or Expression?
| Form | Gives a value? | Must cover every constant? | Falls through? |
|---|---|---|---|
| `case X:` statement | No | No | Yes, without `break` |
| `case X ->` statement | No | No | No |
| Switch expression | Yes | Yes | No |
Exercise
You add a new constant HOLIDAY to the Day enum. The isWeekend method above has no default branch. What happens when you compile, and why is that useful?
Show answer
Compilation fails because the switch expression is no longer exhaustive: HOLIDAY has no case. This is useful because the compiler shows you exactly which code needs updating, instead of letting HOLIDAY silently fall into a wrong default.
Exercise
Write a switch expression that returns the hours worked on a Day: 8 for MONDAY through THURSDAY, 6 for FRIDAY, and 0 for SATURDAY and SUNDAY, with no default branch.
Show answer
static int hours(Day day) { return switch (day) { case MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> 8; case FRIDAY -> 6; case SATURDAY, SUNDAY -> 0; }; }