ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE

Why can a generic method not simply write `new T[10]` for a type parameter T in Java?

  1. A. After erasure, the compiler has no concrete runtime type for T, and array creation needs a known component type at run time to set up the array's type tag
  2. B. Because arrays are a deprecated feature of the language
  3. C. Because T is erased to Object, and even Object[] cannot ever be created
  4. D. Because generics forbid the use of arrays for unrelated security reasons

Correct answer: A. After erasure, the compiler has no concrete runtime type for T, and array creation needs a known component type at run time to set up the array's type tag

Since T's actual type is erased, the JVM would not know what runtime array type to create for `new T[]`, so Java disallows it directly (workarounds use Object[] with an unchecked cast, or reflection).

More OOP with C++ and Java questions

Exams that ask this topic