Multi-Label Cases
Group several labels together.
Grouping Labels
Often several values should trigger the same action. Modern switch lets you list multiple labels in one case, separated by commas, sharing a single branch.
Comma-Separated Labels
Write the values one after another separated by commas before the ->. If any of them matches, the branch runs.
public class Main {
public static void main(String[] args) {
int day = 7;
switch (day) {
case 1, 2, 3, 4, 5 -> System.out.println("Weekday");
case 6, 7 -> System.out.println("Weekend");
}
}
}All lessons in this course
- Arrow-Style switch
- switch as an Expression
- The yield Keyword
- Multi-Label Cases