Sub topic 15
Type Erasure: What Happens at Runtime
You have seen the compiler catch type mistakes in generic code. A natural next question is what the running program knows about those types. In Java, the answer is very little. This chapter explains why.
What Type Erasure Is
Type erasure is the process where the Java compiler removes type arguments after it has checked your code. A type argument is the real type you put in the angle brackets, such as String in List<String>.
The compiler works in two steps:
- It checks that you use the types correctly. For example, it stops you from adding an
Integerto aList<String>. - It erases the type arguments and adds casts where they are needed.
The JVM (Java Virtual Machine) is the program that runs your compiled code. It never sees List<String>. It only sees List.
Seeing Erasure in Action
This program makes two lists with different type arguments and compares their runtime classes.
import java.util.ArrayList;
import java.util.List;
public class ErasureDemo {
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
List<Integer> ints = new ArrayList<>();
System.out.println(strings.getClass() == ints.getClass()); // true
System.out.println(strings.getClass().getName()); // java.util.ArrayList
}
}It prints true because both lists have the same runtime class, ArrayList. The <String> and <Integer> parts are gone.
What T Becomes
Erasure replaces each type parameter with its erasure type:
| Type parameter declaration | Replaced with |
|---|---|
| `T` (no bound) | `Object` |
| `T extends Number` | `Number` |
| `T extends Comparable<? super T>` | `Comparable` |
So the Box<T> class from an earlier chapter behaves at runtime as if you had written this:
public class Box {
private Object value;
public Box(Object value) {
this.value = value;
}
public Object get() {
return value;
}
}When you write String s = box.get();, the compiler quietly adds a cast. The code actually runs as String s = (String) box.get();. You never write that cast yourself, and the compiler has already proved it is safe.
Why This Matters to You
Erasure has a few effects you will run into:
- You cannot ask at runtime whether an object is a
List<String>. You can only check whether it is aList. - Two methods that differ only in their type arguments clash. For example,
void print(List<String> a)andvoid print(List<Integer> a)in the same class will not compile, because both erase toprint(List a). - Some things, such as
new T(), are not allowed. The next chapter covers these limits.
Exercise
A class declares class Holder<T extends Number> { T item; }. After type erasure, what type does the field item have at runtime, and why?
Show answer
It has type Number. Erasure replaces a bounded type parameter with its first bound. T is bounded by Number, so T becomes Number, not Object.
Exercise
Why does new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass() evaluate to true?
Show answer
Type erasure removes the type arguments String and Integer at compile time. At runtime both objects are plain ArrayList instances, so they share the same class object.