J.VII · Java 25 practice question
What does this Java 25 program output when compiled and run?
record Box<T>(T value) {}
void main() {
Box<Box<String>> bbs = new Box<>(new Box<>("ok"));
if (bbs instanceof Box(Box(var s))) {
System.out.println("String " + s);
}
}
Check answer and explanation
Correct answer: String ok
Explanation
The program prints String ok because the compiler infers the type arguments for both the outer and inner Box record patterns as Box<Box<String>>(Box<String>(var s)).
Common trap
Omitting angle brackets on Box looks like a raw type, which makes expecting a compile-time error tempting, but record patterns support type argument inference from the target type. Local variable type inference with var is explicitly allowed in nested record patterns.
Source: JEP 440: Record Patterns — Description
Checked against the cited text on 2026-11-25.