Skip to main content
PreparationZone Logo
PreparationZone
Back to Java

Java · Hard · Pro

Why new T[] is illegal — erasure and heap pollution?

JavaTrickySenior
Model answerPreview

Why new T[] is illegal

Arrays are reified (they know element type at runtime); generic type parameters are erased. Creating new T[n] would let you store wrong types after erasure—heap pollution—and the JVM cannot enforce T at array…