Java generics boundary problem, super and extends keywords

background

Why does JDK5 introduce generics, generics ensure parameter type consistency. What is type consistency?

Assuming an inheritance relationship, A <- B <- C <- D <- E,

ArrayList<C> list;
list.add(new C());
list.add(new D());
list.add(new B());//compile ERROR

It does not destroy the type consistency of the list, because when the parameter type of the list is declared as C, all the reference objects in the list are finally taken out according to the type of C.

With the generic feature, Java should use parameterized types instead of rawtypes. But for compatibility, the use of rawtype is still supported.
Generics have some weird-looking properties that make sense under the assumption that you can't use rawtypes and only use generics.

List

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325236032&siteId=291194637