The meaning of Java generic Java using generics
@author Ixenos
Direct meaning
Guaranteed type safety at compile time
Fundamental significance
A) type safety issues derive from the design of reusable codes, generics guarantee type-safe reuse templates
b) Do not manually force type conversions when using a reuse template
Advantages and disadvantages of three types of generic implementations
- C + +: template mode implementation, fully expanded at compile time and support for partial, type precision, code sharing is poor;
- Java 5: Erase method implementation, only for compile-time type check, wipe at run time, backward compatibility good, code sharing good, type accuracy is very poor;
- C # 2.0: hybrid Implementation, expanded at run time, type precision, code sharing is OK.
- The template method is to generate a specific template for each type, and a generic class will have multiple templates
- The erase method is that all generic type information is erased at compile time, and a generic class has only one template
Q:java Why do I use type erase?
A: For backwards compatibility (referred to as backward compatibility at the binary level, not on the source)
For details, please refer to: reifiable generics and type erasure generics each have what advantages and disadvantages?
Why is Java not implementing a true generic type?
At what stage does Java generic erasure occur, and how do I use the Anti-compilation tool to see the code after the generic erase?
The meaning of Java generic Java using generics