0Pricing
Java Academy · Lesson

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

  1. Arrow-Style switch
  2. switch as an Expression
  3. The yield Keyword
  4. Multi-Label Cases
← Back to Java Academy