Type erasure is a process used by the Java compiler to translate generic code into non-generic bytecode representations. It replaces type parameters with their bounds or Object when no explicit bound is specified.
- Maintains compatibility with Java code written before generics were introduced.
- Inserts type casts and generates bridge methods when required to preserve type correctness and polymorphism.
How Type Erasure Works
During compilation, Java replaces type parameters with their corresponding erased types. The replacement depends on whether the type parameter has an explicit bound.
1. Unbounded Type Parameters
When a type parameter has no explicit bound, the compiler replaces it with Object.
class Box<T>{
private T value;
Box(T value){
this.value = value;
}
T getValue(){
return value;
}
}
After Type Erasure
class Box{
private Object value;
Box(Object value){
this.value = value;
}
Object getValue(){
return value;
}
}
Since T has no explicit upper bound, it is erased to Object.
2. Bounded Type Parameters
When a type parameter has an explicit upper bound, the compiler replaces it with the erasure of its leftmost bound.
class NumberBox<T extends Number>{
private T value;
NumberBox(T value){
this.value = value;
}
T getValue(){
return value;
}
}
After Type Erasure
class NumberBox{
private Number value;
NumberBox(Number value){
this.value = value;
}
Number getValue(){
return value;
}
}
Here, T extends Number specifies Number as the upper bound. Therefore, the field, constructor parameter, and method return type are erased to Number.
Note: When multiple bounds are specified, such as T extends Number & Comparable<T>, the erasure of T is determined by its leftmost bound, Number.
Type Erasure in Action
The Java compiler inserts type casts where required when values are retrieved from generic objects.
Example: Program to demonstrate compiler-inserted type casting.
import java.util.ArrayList;
import java.util.List;
public class Geeks{
public static void main(String[] args){
List<String> list = new ArrayList<>();
list.add("Hello");
// Retrieve the element without explicit casting
String value = list.get(0);
System.out.println(value);
}
}
Output
Hello
Explanation:
- The compiler ensures that only compatible values are added to List<String>.
- After type erasure, the erased return type of get() is Object.
- The compiler inserts a cast to String when retrieving the value.
The erased code is an illustrative representation of the compiler's translation, not the recommended way to write generic Java programs.
Viewing Erased Bytecode
The javap command allows us to inspect compiled Java class files and understand how type erasure affects method signatures and bytecode.
Save the previous program as Geeks.java and execute the following commands:
javac Geeks.java
javap -c Geeks
Relevant Bytecode:
invokeinterface java/util/List.get:(I)Ljava/lang/Object;
checkcast java/lang/String
The get() method returns Object in the erased bytecode, and the checkcast instruction converts the reference to the expected String type after checking its compatibility.
Similarly, after compiling the earlier NumberBox class, execute:
javap -s NumberBox
Output:
class NumberBox<T extends java.lang.Number>{
NumberBox(T);
descriptor: (Ljava/lang/Number;)V
T getValue();
descriptor: ()Ljava/lang/Number;
}
The descriptors show that T is erased to Number.
However, generic declarations may still be retained in class-file metadata. The following command displays this information:
javap -v NumberBox
Limitations of Type Erasure
Type erasure introduces certain restrictions when working with Java generics.
- Cannot create an instance of a type parameter directly using new T().
- Cannot directly create arrays using new T[10] or new List<String>[10].
- Cannot use instanceof to distinguish between generic type arguments, such as checking an Object against List<String>.
- Cannot overload methods whose parameter types have the same erasure, such as process(List<Integer>) and process(List<String>).