Points:
A generic class is a class with one or more type parameters
A generic method is a method with a type parameter
You can require a type parameter to be a subtype of one or more types
A generic type is not to become: when S is a subtype of T is,g<s> and g<t> is not related
By using wildcard characters g<? Extends t> or g<? Super T> You can specify a method to accept an instance of a generic type that takes a string class or parent parameter
Type parameters are erased when generic classes and methods are compiled
Type erasure has many limitations on generics. In particular, you cannot instantiate a generic class or array. cannot be converted to a generic type, or throws a generic type object.
Even if generic classes and methods are erased in the virtual machine, you can also find out how they are declared at run time.
1. Generic type
※the type parameter cannot be instantiated with a basic type. For example, entry<string, int> is not valid in Java.
When constructing a generic class object, you can omit the type parameter in the constructor.
1 New Entry<> ("1", 1);
2. Generic method
When declaring an anti-wash method, the type parameter is placed after the modifier, before the return type. When called, do not specify a type parameter. It can be inferred from the parameter and return type of the method.
can also be displayed at the time of the call to write
1 Public Static void int int j); 2 Arrays.<string>swap (friends,0,1);
3. Type limit
The type parameters of a generic class or method need to meet certain requirements, or you need to be able to invoke some common methods . You can qualify the type.
T extends Autocloseable
Type parameters can have multiple qualifications: T extends Runnable & autocloseable
※ Multiple interfaces can be qualified, but only one of them is a class. If a qualification is a class, it must be placed first in the qualified list.
Java core Technology notes for busy people (6, generic programming)