Type Erasure
How generics work at runtime
Interview Relevant: Understanding limitations
6 min read
Type Erasure
Java generics use type erasure, meaning generic type information is removed at runtime.
Code Examples
Type erasure at runtime
java
1
2List<String> l1 = new ArrayList<>();
3List<Integer> l2 = new ArrayList<>();
4
5System.out.println(l1.getClass() == l2.getClass()); // true
6 Use Cases
- Backward compatibility
- Understanding runtime behavior
Common Mistakes to Avoid
- Expecting generic info at runtime
- Using instanceof with generics