Reproduced in: 52092692
Before the interview encountered the interviewer asked the role of generics, just said about their own understanding,
In this reprint record, deepen the impression
Generic Benefits:
Generics Simple to use
The primary goal of type-safe generics is to implement type safety for Java. Generics enable the compiler to know what the qualified type of an object is, so that the compiler can verify the type at a high level
Elimination of forced type conversions makes the code more readable and reduces the chance of errors
The benefits of introducing generics into the Java language are simple and secure. The benefit of generics is that it checks for type safety at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code.
The implementation principle of generic type
The implementation of generics is by type Erase technology type erase is done at compile time, that is, the compiler will erase the generic type parameters into its qualified type, and if not, then force the type to convert to the corresponding type when it is obtained. There is no information for generics during the run, and therefore no optimizations.
Generics do not consider inheritance
Can the list type be assigned a value of list type?
The answer is no.
Although the string type can be used as object type after assignment, the problem still occurs
Suppose the following code
The role and benefits of Java generics