CKTIS / Java
Generic type erasure in Java
Java checks type arguments at compile time, but `ArrayList<String>` and `ArrayList<Integer>` do not have separate runtime classes. This distinction explains restrictions on arrays and `instanceof`.
Version: Java SE 21
Syntax
Type erasure is a compiler rule, not a Java keyword. In specification notation, |X| means the erasure of X: |List<String>| = List; |T| = Object for an unbounded T; |T| = Number for T extends Number & Comparable<T>; |T[]| = |T|[].Generic type erasure, often called type erasure, is a compilation rule, not a keyword. The compiler uses type arguments when checking your code. The erasure of List<String> is List. A type variable T erases to the erasure of its leftmost bound, or to Object if no bound is declared. The compiler inserts casts where needed and can generate bridge methods to preserve overriding.
ArrayList<String> and ArrayList<Integer> are distinct source types, but the instances created here return the same Class object from getClass(). Reflection can read generic declaration signatures, but getClass() alone cannot tell you an instance’s type argument. You also cannot overload m(List<String>) and m(List<Integer>) in the same class: both signatures erase to m(List).
new List<String>[2] is a compile-time error because its element type, List<String>, is not fully available at run time (it is not reifiable). The JVM checks the component type when storing into an array, but it cannot check the String type argument. new List<?>[2] is allowed: List<?> is reifiable and promises no particular list element type.
If value has static type Object, value instanceof List<String> is a compile-time error because the corresponding cast would be unchecked. value instanceof List<?> checks whether the object implements List, not what the list contains. Since Java SE 16, some tests with concrete type arguments are legal: if x is declared as List<Integer>, x instanceof ArrayList<Integer> is valid because the cast would not be unchecked. At run time the test checks for ArrayList, not for the Integer type argument or the contents.
Examples
import java.util.ArrayList;
import java.util.List;
public class TypeErasureDemo {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<>();
ArrayList<Integer> numbers = new ArrayList<>();
System.out.println(strings.getClass() == numbers.getClass());
Object value = strings;
System.out.println(value instanceof List<?>);
// boolean invalidTest = value instanceof List<String>;
List<Integer> x = numbers;
System.out.println(x instanceof ArrayList<Integer>);
List<?>[] lists = new List<?>[2];
lists[0] = strings;
lists[1] = numbers;
System.out.println(lists.length);
// List<String>[] invalidArray = new List<String>[2];
}
}Result
true true true 2
The first three lines are `true`: same class, a match for `List<?>`, and a match for `ArrayList<Integer>` given the static type. `2` is the array length. Uncomment each invalid statement separately to see its compile-time error.
Related lessons
- Java · Lesson 58 · Basic · Version: 2
- Java · Lesson 36 · Intermediate · Version: 2
Sources
- Java Language Specification, Java SE 21 — §4.6 Type Erasure
- Java Language Specification, Java SE 21 — §4.7 Reifiable Types
- Java Language Specification, Java SE 21 — §8.4.8.3 Requirements in Overriding and Hiding
- Java Language Specification, Java SE 21 — §10.5 Array Store Exception
- Java Language Specification, Java SE 21 — §15.10.1 Array Creation Expressions
- Java Language Specification, Java SE 21 — §15.20.2 The instanceof Operator
- Object.getClass() — Java SE 21 & JDK 21 API
- Java Virtual Machine Specification, Java SE 21 — §4.7.9 The Signature Attribute
- The Java Tutorials — Type Erasure