ProLesson 6.5Advanced3 min readUpdated 30 October 2026
Type erasure and its limits
How Java implements generics by erasing types after compiling, and why that explains every strange generics restriction.
Generics exist only at compile time. After checking your types, the compiler erases them: in the bytecode, List<String> and List<Integer> are both just List. This design kept new generic code compatible with old Java code, and it explains every generics rule that seems odd.
01Seeing erasure
Erasure.java
import java.util.ArrayList;
import java.util.List;
public class Erasure {
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
List<Integer> numbers = new ArrayList<>();
System.out.println(strings.getClass() == numbers.getClass());
System.out.println(strings.getClass().getName());
}
}true java.util.ArrayList
At run time there's only one ArrayList class. The JVM doesn't know which element type a list was declared with.
Loading the rest of the lesson…