J.VII · Java 25 practice question
What is the result of compiling this Java 25 code?
void main() {
Object obj = 10;
switch (obj) {
case Character c:
case Integer i:
System.out.println("matched");
break;
default:
break;
}
}
Check answer and explanation
Correct answer: It fails to compile because stacking multiple pattern case labels on one statement group is not permitted
Explanation
It fails to compile because stacking multiple pattern case labels on one statement group is not permitted, even if the pattern variables are unused in the body.
Common trap
Because neither c nor i is read in the body, it is tempting to think the compiler permits stacking them like constant labels, but the grammar and fall-through rules forbid multiple pattern labels on a single group. Autoboxing 10 to Integer is valid and would not cause it to print nothing.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.