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.
βCoffee Chat Question
Concept Made Simple
βHow does generic type erasure affect runtime behavior, and what breaks because of it?β
π§ Mind Map Answer
Remember It Faster
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).
β¨οΈHands-on Keyboard
Learn by Doing
List<String> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
System.out.println(a.getClass() == b.getClass()); // true β both ArrayListtrue
π₯What If?
Think Beyond the Expected
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:
β οΈ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
- 1What is heap pollution and how does @SafeVarargs relate?
- 2How does PECS (Producer Extends, Consumer Super) guide wildcards?
- 3Why can't you catch a generic exception type?
π§©Related Technologies
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?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.