CKTIS / Java
Type erasure and class tokens in Java
Type erasure gives `ArrayList<String>` and `ArrayList<Integer>` the same runtime class, but reflection can still read a type argument from a field declaration.
Version: Java SE 21
Syntax
List<String> names = new ArrayList<>();
Class<String> stringToken = String.class;
Class<?> listToken = List.class;
Class<?> runtimeClass = names.getClass();
boolean isList = value instanceof List<?>; // value has static type Object
Type declaredType = field.getGenericType(); // field is a java.lang.reflect.FieldThe compiler checks generic types before erasure. ArrayList<String> and ArrayList<Integer> both erase to ArrayList, while a type variable erases to the erasure of its leftmost bound (Object if unbounded). Objects created directly from either parameterization return the same Class object from getClass().
A reifiable type is fully available at run time. List<?> is reifiable; List<String> is not. For value declared as Object, value instanceof List<?> is legal and tests only whether the object implements List; value instanceof List<String> does not compile. Java SE 21 allows some parameterized instanceof tests based on the operand's static type, but those tests still do not check erased type arguments.
A class token is a Class object. The literal String.class has type Class<String>, and List.class represents the List interface without type arguments. Neither List<String>.class nor List<?>.class is a valid literal: reifiability alone is not enough. Class.cast checks compatibility with the represented class or interface, not the types of a list's elements.
Erasure does not discard all generic information. For a field declared as List<String>, the class file's Signature attribute preserves the declared type. Field.getType() returns List.class, while Field.getGenericType() returns a ParameterizedType from which you can read the String argument. This describes the field declaration, not the object currently stored in it.
Examples
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
public class TypeErasureDemo {
private List<String> names = new ArrayList<>();
public static void main(String[] args) throws NoSuchFieldException {
var strings = new ArrayList<String>();
var integers = new ArrayList<Integer>();
System.out.println("Same runtime class: " + (strings.getClass() == integers.getClass()));
System.out.println("Matches ArrayList.class: " + (strings.getClass() == ArrayList.class));
Field field = TypeErasureDemo.class.getDeclaredField("names");
System.out.println("Erased field type is List: " + (field.getType() == List.class));
// This field is known to have a parameterized declaration.
ParameterizedType declaredType = (ParameterizedType) field.getGenericType();
System.out.println("Declared type argument is String: "
+ (declaredType.getActualTypeArguments()[0] == String.class));
}
}Result
Same runtime class: true Matches ArrayList.class: true Erased field type is List: true Declared type argument is String: true
The first two results concern the objects' runtime class. The last two contrast the field's erased type with the type argument recorded in its declaration.
Related lessons
- Java · Lesson 13 · Advanced · Version: 2
Sources
- Java SE 21 Language Specification §4.6 — Type Erasure
- Java SE 21 Language Specification §4.7 — Reifiable Types
- Java SE 21 Language Specification §15.8.2 — Class Literals
- Java SE 21 Language Specification §15.20.2 — The Type Comparison Operator instanceof
- Java SE 21 Virtual Machine Specification §4.7.9 — The Signature Attribute
- Java SE 21 API — Object.getClass()
- Java SE 21 API — Class.cast(Object)
- Java SE 21 API — Field
- Java SE 21 API — ParameterizedType.getActualTypeArguments()