J.VII · Java 25 practice question
What does the following Java 25 `switch` statement using pattern matching print?
void main() {
Object item = 42;
switch (item) {
case String s -> System.out.println("text");
case Integer i -> System.out.println(i * 2);
default -> System.out.println("none");
}
}
Check answer and explanation
Correct answer: 84
Explanation
`item` is an `Integer` with value `42`, so `case Integer i` matches and prints `42 * 2`, which is `84`.
Common trap
Printing `42` ignores the `* 2` action, and `default` is not reached because `Integer i` matches.
Source: JEP 441: Pattern Matching for switch — Summary
Checked against the cited text on 2026-11-27.