Java Generics 简明教程
Java Generics - Type Erasure
泛型用于在编译时进行更严格的类型检查并提供泛型编程。为了实现泛型行为,Java 编译器应用类型擦除。类型擦除是一个编译器将泛型参数替换为实际类或桥接方法的过程。在类型擦除中,编译器确保不创建额外的类且没有运行时开销。
Generics are used for tighter type checks at compile time and to provide a generic programming. To implement generic behaviour, java compiler apply type erasure. Type erasure is a process in which compiler replaces a generic parameter with actual class or bridge method. In type erasure, compiler ensures that no extra classes are created and there is no runtime overhead.
Type Erasure rules
-
Replace type parameters in generic type with their bound if bounded type parameters are used.
-
Replace type parameters in generic type with Object if unbounded type parameters are used.
-
Insert type casts to preserve type safety.
-
Generate bridge methods to keep polymorphism in extended generic types.