The generic

Only object types can be passed, not basic data types

Refers to any reference data type [is an unknown type used, specific at the time of use to determine]

A generic class

Define the class

img

Any type can be passed in when used

Generic method

img

The following analogy can be used when using data types or objects

The basic use of generic wildcards

Generic wildcards:

The use of generic wildcards: If we want our parameters to accept generics of any type, we can use the generic wildcard to do so.

== Note: the set defined by the generic wildcard does not support the add or delete operation, only supports the read operation ==

img

Restricted generics?

img

Qualified wildcards always include themselves

Upper bound type wildcard: The add method is limited

Lower bound type wildcard: The GET method is restricted

If you want to retrieve data from a data type, use? Extends wildcard

If you want to write an object to a data structure, use? Super wildcard

If you want to get both, don’t use wildcards

You cannot declare both the upper and lower bounds for generic wildcards

Generic erasure

Java generics are pseudo-generics, which are processed at compile time as our ordinary methods and classes. So when we write a piece of code with generics that looks like it has a type but doesn’t have one, it’s still an object type. The mechanism is type erasure, erasure rules:

If the generic type does not specify a specific type, use Object as the primitive type. If there is a qualified type < T exnteds XClass >, use XClass as the original type. If there are more than one qualification < T exnteds XClass1 & XClass2 >, use the first bound type XClass1 as the primitive type;

If there is a problem, please point it out!