J.VII · Java 25 practice question
What does the following Java 25 program print when matching a `record` inside a `switch` expression?
record Point(int x, int y) {}
void main() {
Object o = new Point(3, 4);
int sum = switch (o) {
case Point(int x, int y) -> x + y;
default -> 0;
};
System.out.println(sum);
}
Check answer and explanation
Correct answer: 7
Explanation
`o` matches the record pattern `Point(int x, int y)` with `x = 3` and `y = 4`, so `x + y` evaluates to `7`.
Common trap
`default` is not taken because `Point` matches, and `12` multiplies instead of adding.
Source: JEP 441: Pattern Matching for switch — Summary
Checked against the cited text on 2026-11-27.