What is type erasure in Java Generics
π‘ Concept: Type Erasure
Type erasure is the process by which the Java compiler removes all generic type information during compilation, replacing them with raw types or bounds.
π Quick Intro
Java Generics provide type safety at compile time but due to type erasure, generic type parameters are removed at runtime. This ensures backward compatibility with older Java versions.
π§ Analogy
Think of Generics like labels on a folder (e.g., "Invoices<String>"). Type erasure is like removing the label before putting it in a drawerβat runtime, the JVM doesnβt know what type it originally referred to.
π§ Technical Explanation
- At compile-time, generics ensure type safety.
- During compilation, type parameters are replaced with their bounds (or Object if unbounded).
- Resulting bytecode uses raw types, and actual type info is not retained at runtime.
- Because of type erasure, method overloading based solely on generic types is not allowed.
- Reflection cannot access actual generic types without explicit annotations or metadata.
π― Use Cases
- β Writing type-safe collections and utilities at compile-time
- β Ensuring backward compatibility with pre-Java 5 bytecode
- β Understanding limitations of generics in reflection and method overloading
π» Example: Type Erasure in Generics
public class Box<T> {
T value;
public void set(T val) { value = val; }
public T get() { return value; }
}
// After compilation, becomes:
public class Box {
Object value;
public void set(Object val) { value = val; }
public Object get() { return value; }
}

β Interview Q&A
Q1: What is type erasure?
A: It's the removal of generic type info during compilation.
Q2: Why does Java use type erasure?
A: For backward compatibility with older versions.
Q3: Is generic type info available at runtime?
A: No, it is erased at compile-time.
Q4: What is the bound of an unbounded generic type?
A: Object.
Q5: Can you overload methods with different generic types?
A: No, due to erasure.
Q6: Can reflection access generic types directly?
A: No, not without annotations or tricks.
Q7: What happens to <T> at runtime?
A: It is replaced with its bound (or Object).
Q8: Can you create instances of generic types at runtime?
A: No, because type info is erased.
Q9: What is the impact of type erasure on performance?
A: Minimal, mostly affects type introspection.
Q10: How to retain generic type info at runtime?
A: Use annotations or explicitly pass Class objects.
π MCQs
Q1. What is type erasure in Java?
- Runtime cast failure
- Removal of generic types during compilation
- Looping through lists
- Type conversion issue
Q2. Why was type erasure introduced?
- To simplify syntax
- For runtime performance
- To maintain backward compatibility
- To replace raw types
Q3. What replaces unbounded type parameters?
- int
- T
- Object
- Void
Q4. Is generic info available at runtime?
- Yes
- No
- Only in Spring
- Only with reflection
Q5. Can you overload methods on generic type?
- Yes
- No
- Only static methods
- Only if generic is bounded
Q6. Which concept does type erasure affect?
- Loops
- Interfaces
- Reflection and overloading
- Enums
Q7. Which version introduced generics?
- Java 7
- Java 1.2
- Java 5
- Java 8
Q8. After erasure, what does List<String> become?
- List<Object>
- List
- ArrayList
- Map
Q9. Which is a workaround to get type info?
- Use hashCode()
- Use new T()
- Use Class<T> param
- Use clone()
Q10. Does erasure impact compiled bytecode?
- No
- Yes
- Sometimes
- Only in methods
Tags:
- #Articles
- #Webd
- #Java
- #Type
- #Erasure
- #Generics
- #FullStackPrep