Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Generics were introduced to Java in 2004 with a strict architectural requirement: backwards compatibility. Pre-existing compiled Java 1.4 .class files had to seamlessly link with newly compiled generic code without changing the JVM bytecode format.
To achieve this, the Java compiler implements generics using a mechanism known as Type Erasure.
During compilation, javac:
Object if unbounded.If bounded (<T extends Number>), T erases to Number instead of Object.
When a class extends or implements a generic supertype with a concrete type argument, method signatures in bytecode differ after erasure. The compiler automatically synthesizes a hidden Bridge Method:
Because setData(String) does not match setData(Object), the compiler injects a synthetic bridge method:
Because type arguments are erased at runtime:
instanceof on generic types:if (obj instanceof List<String>) ❌ COMPILE ERROR. Use if (obj instanceof List<?>) instead.
T obj = new T(); ❌ COMPILE ERROR.
T[] array = new T[10]; ❌ COMPILE ERROR.
private static T instance; ❌ COMPILE ERROR.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 1 questions. Each question has one correct answer.