J.III · Java 25 practice question
Given this Java 25 program using a parameterized type's constructor and member, what is printed?
public class Main {
static class Box<T> {
final T item;
Box(T item) { this.item = item; }
T unwrap() { return item; }
}
public static void main(String[] args) {
Box<String> b = new Box<>("java25");
System.out.println(b.unwrap().length());
}
}
Check answer and explanation
Correct answer: 6
Explanation
In Box<String>, substituting T with String gives unwrap() the return type String, so calling length() on "java25" compiles and prints 6.
Common trap
The string "java25" has 6 characters (j, a, v, a, 2, 5), not 5. Because b has the parameterized type Box<String>, unwrap() has static type String rather than Object.
Source: JLS SE 25 §4.5.2. Members and Constructors of Parameterized Types
Checked against the cited text on 2026-11-25.