J.VII · Java 25 practice question

What does the following Java 25 `switch` statement print when matching `Double`?

void main() {
    Number n = 3.5;
    switch (n) {
        case Integer i -> System.out.println("I");
        case Double d -> System.out.println("D");
        default -> System.out.println("N");
    }
}
Choose one answer
Check answer and explanation

Correct answer: D

Explanation

`3.5` boxes to `Double`, matching `case Double d` and printing `D`.

Common trap

`3.5` is a `Double`, not an `Integer`, and matches before `default`.

Source: JEP 441: Pattern Matching for switch — Summary
Checked against the cited text on 2026-11-27.

Report an error in this question