How does generic type erasure affect runtime behavior, and what breaks because of it?
Reviewed by Gurusankar M.
Short Answer
Generics are compile-time only; the JVM erases type parameters to their bounds (often Object). So you can't do new T[], instanceof List<String>, or have overloads differing only by generic type — and unchecked casts can cause runtime ClassCastException.
On this page
Concept Made Simple
Coffee Chat Question
“How does generic type erasure affect runtime behavior, and what breaks because of it?”
Remember It Faster
Mind Map Answer
At runtime List<String> and List<Integer> are both just List. The compiler inserts casts and checks at compile time, then throws the type info away (erasure).
- Can't
- new T[], T.class, instanceof List<String>
- Why
- type param gone at runtime
- Leak
- raw types → heap pollution
Learn by Doing
Hands-on Keyboard
List<String> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
System.out.println(a.getClass() == b.getClass()); // true — both ArrayListtrue
Think Beyond the Expected
What If?
How do frameworks recover the generic type at runtime if it's erased?
Type parameters on a class/field/method signature ARE retained in metadata. Jackson/Spring use a 'super type token' (TypeReference / ParameterizedTypeReference) — an anonymous subclass — to read that retained signature via reflection.
Real World
Deserializing JSON into List<Dto> with Jackson requires `new TypeReference<List<Dto>>(){}` precisely because the bare `List<Dto>.class` type is erased.
Interviewer's Expectation
Keywords they're listening for:
- compile-time only
- erasure to bound/Object
- no new T[]/instanceof generic
- super type tokens
- heap pollution via raw types
Common Mistakes
- Mixing raw and generic types
- Expecting instanceof to check the type argument
- Creating generic arrays directly
Best Practices
- Avoid raw types; keep -Xlint:unchecked clean
- Use TypeReference/ParameterizedTypeReference for generic deserialization
- Follow PECS for flexible APIs
Follow-up Questions
- What is heap pollution and how does @SafeVarargs relate?
- How does PECS (Producer Extends, Consumer Super) guide wildcards?
- Why can't you catch a generic exception type?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Generics (Core Java) Interview question: "How does generic type erasure affect runtime behavior, and what breaks because of it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair