1, Origin
1. Generic from jdk1 5 is added to the Java language. Its main purpose is to solve the problem of ClassCastException. There will always be security risks during the downward transformation of objects. It can compile, run and solve most of the errors
2. The essence of generics is that the parameters and return values of attributes or methods in a class can be dynamically determined when the object is instantiated.
Avoid object coercion
2, Foundation
1. Point this T represents the general uppercase letter of any class derived from the Object class
Advantages 1. Object needs to be forced to convert generic types. 2. If the incoming type is wrong, an error will be reported during compilation
Common elements represent:
class MorePoint<T,U,A,B,C>{ }
- E-Element, commonly used in java Collection, such as List,Iterator,Set
- K. V - Key, Value, representing the Key Value pair of Map
- N - Number
- T - Type, such as String, Integer, etc
2,? And T
The function of T is to limit the available types of generic classes. The types of generic classes must implement or inherit anyClass, regardless of whether anyClass is an interface or class
? Is a type wildcard. Its main function is to restrict the type implementation or inheritance of a generic class when creating a generic class object
·? Extensions class: set the upper limit of generics|- For example: "? Extensions Number": it means that the generic type can only set Number or subclasses of Number
·? super class: set the lower limit of generics: | - for example: "? super String": only String or its parent class can be used.
3. Other issues
interface IPoint<S>{ public String print(S s) ; } class PointImpl implements IPoint<T> // Error T cannot appear in the interface alone class PointImpl implements IPoint<String> //correct class PointImpl<T> implements IPoint<T> //The interface method in the subclass is consistent with the type of iPoint < T > t class InfoImpl<T,K,U> implements Info<U> //Generic class public static <T>list<T> aslist(T...a) //<T> Represents a generic method. T represents a generic type public static <T> void StaticMethod(T a) // public class StaticFans { //Static function public static <T> void StaticMethod(T a){ Log.d("harvic","StaticMethod: "+a.toString()); } //Ordinary function public <T> void OtherMethod(T a){ Log.d("harvic","OtherMethod: "+a.toString()); } } //Static method StaticFans.StaticMethod("adfdsa");//Usage method 1 must be an implicit type derived from the Object class StaticFans.<String>StaticMethod("adfdsa");//Usage 2 //conventional method StaticFans staticFans = new StaticFans(); staticFans.OtherMethod(new Integer(123));//Use method I staticFans.<Integer>OtherMethod(new Integer(123));//Usage 2
4. < T > T and class <? > clazz
< T > indicates the incoming type, and t indicates the return type (such as String and int)
Class passes the class object of the class, passing in the type (T aa)
Further instructions on( Reference from)
It is reasonable to say that a T represents the return value. Why do you need one? This means that this method is declared as a generic method
But there is another situation. It is also a generic method. Why don't the following getName methods need to be declared?
public class Test1<T> { private T name; public T getName(T name){ return name; } public static <T> void printA(T a){ System.out.println(a); } }
Explanation: for declared classes (Test1 is the declared class), there is no need to declare generic methods. For static methods, they do not belong to the class, so they are equivalent to undeclared classes, so they need to be declared as generic methods
Classes like Test2 must declare generic methods
public class Test2 { public <T> T test1(T A){ System.out.println(A); System.out.println(A.getClass()); return A; } }
3, Generic erase
Procedure: check the generic type (Reference) in the code, and then erase the type before compiling.
Any type can be set at runtime, which is not limited by type, for example:
Print:
Because of the problem of type erasure, all generic type variables will eventually be replaced with the original type. This raises a question. Since they are all replaced with original types, why don't we need to cast them when we get them?
Automatic type conversion
Take a look at the ArrayList and get methods:
-
public E get(int index) { RangeCheck(index); return (E) elementData[index]; }
You can see that before return, strong conversion will be performed according to generic variables.
Compile time generic erase original type minimum parent type knows Object
Static methods and static variables in generic classes cannot use generic type parameters declared by generic classes
Therefore, the virtual machine skillfully uses the bridge method to solve the conflict between type erasure and polymorphism.
Generic type parameters cannot be used in catch statements of Java exception handling. Indistinguishable anomaly
The main difference between the primitive type List and the parameterized type List < Object > is that the compiler will not check the type safety of the primitive type at compile time, but will check the parameterized type.