ReviseAlgo Logo

Generics

Type Erasure

Analyze Type Erasure compiler processes, bridge method generations, and runtime parameter limitations.

Interview: Focuses on runtime type parameters removal, synthetic bridge methods, and compile-time casts.

Last Updated: June 13, 2026 10 min read

Type Erasure is the process where the Java compiler removes all generic type parameter information during compilation. This ensures bytecode backward compatibility with pre-Java 5 applications.

Type Removal

Replaces type parameters with their first bound (e.g. Number) or Object if unbounded.

Bridge Methods

Generates synthetic bridge methods automatically during compilation to preserve polymorphism in class inheritance.

Compiler Casts

Inserts explicit type casts into the generated bytecode to guarantee type safety at runtime.

Bridge Methods & Bytecode Restrictions

Type erasure introduces runtime limitations that developers must manage:

  • No Runtime Type Queries: You cannot run instanceof List<String> because the type arguments are erased. Only raw queries like instanceof List are allowed.
  • Bridge Methods: If a class extends a generic class (e.g. class StringNode extends Node<String>), the compiler inserts a synthetic bridge method: public void setData(Object o) { setData((String) o); } to override the erased base method.

Common Pitfalls

  • Trying to check generic types via instanceof: Writing if(list instanceof List<String>), which causes a compile-time error.
  • Overloading conflicts: Declaring two methods that differ only by type parameters (e.g. void print(List<String> l) and void print(List<Integer> l)), which fail compilation because both erase to the same signature.

Best Practices

  • Pass Class references: Pass explicit class tokens (e.g. Class<T> clazz) to methods if runtime type checks or object instantiations are required.
  • Use SafeVarargs: Use the @SafeVarargs annotation on methods that accept generic varargs parameters to suppress heap pollution warnings.

Interview-Relevant Information

Q1: What is type erasure in Java Generics?
Answer: It is the compiler process where all generic type parameter information is removed during compilation, replacing type parameters with their first bound (or Object) and inserting casts. This ensures backward compatibility with pre-Java 5 bytecode.

Q2: Why are bridge methods generated by the compiler?
Answer: Bridge methods are synthetic methods generated to preserve polymorphism. When a class implements a parameterized method, type erasure changes the method signature in the parent class to accept Object. The compiler inserts a bridge method in the subclass to map the raw signature to the typed signature.

Quick Checklist

Can you define type erasure, explain why method overloading based only on type parameters fails, and describe the purpose of bridge methods? If yes, you understand type erasure.

Use Cases

Analyzing framework bytecode during performance debugging.

Building serializers that map JSON strings to class instances using class tokens.

Common Mistakes

Attempting to check generic types using instanceof at runtime.

Writing overloaded methods that differ only by generic type parameters.