MediumExperience level: 3-5 years · 8-15 years1 min read

How does generic type erasure affect runtime behavior, and what breaks because of it?

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftGoogle
#generics#type erasure#reflection#heap pollution
Report issue

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

java
List<String> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
System.out.println(a.getClass() == b.getClass()); // true — both ArrayList
Output
true

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

  1. What is heap pollution and how does @SafeVarargs relate?
  2. How does PECS (Producer Extends, Consumer Super) guide wildcards?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.