What is type erasure and what limitations does it bring?
Short answer
Type erasure: the compiler checks generic types, then erases them. At runtime List<String> and List<Integer> are the same class.
3 points to say:
- Erasure keeps pre-Java 5 code working alongside new code.
- So you can't write
new T()ornew T[n]. - To know the type at runtime, pass a
Class<T>orSupplier<T>.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.