Erasure and reifiable types
What you will learn
Type erasure is part of Java’s translation of generic code: many type arguments are not represented in ordinary runtime objects. Reifiable types retain enough runtime type information for operations that require it. Understanding the distinction prevents unsafe casts, raw-type bugs and invalid generic arrays.
- Explain erasure, reifiability and the limits of runtime tests.
- Recognize heap pollution and the role of compiler-generated bridge methods.
Use a fresh directory per example. Save as Main.java, compile with javac --release 21 Main.java, then run java Main. Only JDK standard-library dependencies are needed. List, Map and Set operations used here are brief previews of later collection chapters.
Inspect an erased class and a safe wildcard test
import java.util.*;public class Main { static final class Box<T> { private final T value; Box(T value) { this.value = value; } T get() { return value; } } public static void main(String[] args) { Box<String> a = new Box<>("Map"); Box<Integer> b = new Box<>(3); System.out.println(a.getClass() == b.getClass()); Object x = List.of("Map", "Atlas"); if (x instanceof List<?> list) { System.out.println(list.size()); System.out.println(list.get(0) instanceof String); } }}Expected output:
true2trueThe box instances share a runtime class. The wildcard test establishes list membership, not that every element is a string.
Rules and mental model
For a type variable, erasure uses its leftmost bound, or Object when unbounded. Parameterized types generally erase to the underlying class or interface. The compiler can insert casts at retrieval sites and generate bridge methods to preserve overriding across erased signatures. Some generic declaration metadata is retained for reflection; erasure does not mean every generic signature disappears. A reifiable type such as List<?> can be tested at runtime; a general Object cannot be tested as List<String> because element arguments are not available in that form. Arrays carry their runtime component type and check stores, so direct creation of new List<String>[n] or new T[n] is rejected. Raw types exist for legacy compatibility; using them to insert the wrong element can pollute a typed collection, with a later ClassCastException. A generic varargs parameter also deserves care because its array can expose non-reifiable elements. Prefer typed APIs and lists instead of unchecked casts.
Observe overriding through an erased generic interface
public class Main { interface Reader<T> { T read(); } static final class TextReader implements Reader<String> { public String read() { return "Map"; } } public static void main(String[] args) { Reader<String> reader = new TextReader(); System.out.println(reader.read()); int bridges = 0; for (java.lang.reflect.Method method : TextReader.class.getDeclaredMethods()) { if (method.isBridge()) bridges++; } System.out.println(bridges); }}Expected output:
Map1The local javac emits a synthetic bridge for the erased interface method. The count is an observation of this compiler/fixture, not a universal count for every generic class.
Common mistakes
Do not claim getClass distinguishes Box<String> and Box<Integer>. Do not suppress raw or unchecked warnings as a normal solution. Overloads differing only in List<String> versus List<Integer> clash after erasure.
Practice
Implement countStrings(Object value). If value is a List<?> count its String elements; otherwise return zero. Test a mixed list, an empty list and a non-list. Do not cast to List<String>.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Erasure and reifiable types — knowledge check
Apply the chapter contracts and reason about Java 21-compatible examples.
Do Box<String> and Box<Integer> have distinct runtime classes in this lesson?
What does instanceof List<?> establish?
Why are f(List<String>) and f(List<Integer>) illegal overloads together?