Multi-Label Cases
Group several labels together.
Multi-Label Cases is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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");
}
}
}Vowel Detection
A classic use: grouping all vowels into one branch instead of writing five separate cases.
public class Main {
public static void main(String[] args) {
char c = 'o';
switch (c) {
case 'a', 'e', 'i', 'o', 'u' -> System.out.println("Vowel");
default -> System.out.println("Consonant");
}
}
}Multi-Label in Expressions
Multi-label cases work in switch expressions too, returning the same value for any of the listed labels.
public class Main {
public static void main(String[] args) {
int month = 7;
String season = switch (month) {
case 12, 1, 2 -> "Winter";
case 3, 4, 5 -> "Spring";
case 6, 7, 8 -> "Summer";
default -> "Autumn";
};
System.out.println(season);
}
}String Labels Grouped
String values can also be grouped. Here several command spellings map to one action.
public class Main {
public static void main(String[] args) {
String cmd = "quit";
switch (cmd) {
case "quit", "exit", "q" -> System.out.println("Closing");
case "help", "h" -> System.out.println("Showing help");
default -> System.out.println("Unknown command");
}
}
}Enum Labels Grouped
Enum constants can share a branch as well, which keeps category logic compact.
public class Main {
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
public static void main(String[] args) {
Day d = Day.SAT;
String kind = switch (d) {
case SAT, SUN -> "Rest";
default -> "Work";
};
System.out.println(kind);
}
}No Duplicate Labels
Each label may appear only once across the whole switch. Listing the same value in two branches is a compile error, because the compiler could not decide which branch to run.
public class Main {
public static void main(String[] args) {
int n = 2;
switch (n) {
case 1, 2 -> System.out.println("Low");
case 3, 4 -> System.out.println("High");
default -> System.out.println("Other");
}
}
}Compared to Old Fall-Through
In the old colon style, grouping required empty fall-through cases stacked above one shared body. The comma syntax is far clearer and avoids accidental fall-through.
public class Main {
public static void main(String[] args) {
int code = 3;
switch (code) {
case 1, 3, 5, 7, 9 -> System.out.println("Odd");
case 0, 2, 4, 6, 8 -> System.out.println("Even");
}
}
}Grouping With Blocks
A grouped case can use a block body too. Any of the matching labels runs the whole block.
public class Main {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A', 'B' -> {
System.out.println("Pass");
System.out.println("Well done");
}
default -> System.out.println("Try again");
}
}
}Combining With yield
Grouped labels and yield work together when a shared branch needs computation.
public class Main {
public static void main(String[] args) {
int n = 4;
int points = switch (n) {
case 1, 2, 3 -> 10;
case 4, 5, 6 -> {
int bonus = n * 2;
yield 20 + bonus;
}
default -> 0;
};
System.out.println(points);
}
}Readability Win
Multi-label cases shrink long lists of repetitive branches into a few clear groups, making intent obvious at a glance.
public class Main {
public static void main(String[] args) {
int http = 204;
String group = switch (http / 100) {
case 1, 2, 3 -> "OK-ish";
case 4, 5 -> "Error";
default -> "Unknown";
};
System.out.println(group);
}
}Quick Check
How do you make several values run the same switch branch in modern Java?
Recap
You learned multi-label cases:
- List values comma-separated before
-> - Works for ints, chars, Strings, and enums
- No accidental fall-through, unlike the old style
- Each label may appear only once
- Combine with blocks and
yieldwhen needed
Frequently asked questions
Is the “Multi-Label Cases” lesson free?
Yes — the full text of “Multi-Label Cases” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Multi-Label Cases”?
Group several labels together. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Multi-Label Cases” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrow-Style switch
- switch as an Expression
- The yield Keyword
- Multi-Label Cases