Generic code vs. virtual machines:
Automatically provide original type, erase type variable, replace with qualified type (none with Object)
When a generic method is called, the compiler enforces type conversions on the returned object
A generic class erase causes a method that was originally overridden in a generic class subclass to become overloaded. (The argument type becomes object) causes the subclass to have a method that has two parameters of the same method name, which would have called only the method (polymorphic) of the subclass, and the result might have called a method with the parameter objec in the generic superclass. The compiler automatically joins the bridge method in the subclass:
public void Setsecond (object a) {Setsecond ((Date) a)}
Similarly, in a method with a return type, two methods of the same parameter type are generated. The virtual machine determines a method based on the parameter type and return type.
A method that overrides another method can specify a more restrictive return type.
1 virtual machines do not have generics, only ordinary classes and methods; 2 All type parameters are replaced with their qualified Type 3 bridge method to maintain polymorphism 4 to maintain security, insert forced type conversions if necessary
Enables interoperability between generic and legacy code.
Constraints and limitations of generics (mostly caused by erasure):
1. You cannot instantiate a type parameter with a base type, OBJEC cannot store a double value.
2. The run-time type query only applies to the original type. When you use a INSTANCEOF,GETCALSS or a generic type of coercion, it is the effect of the type erase.
3. Cannot create an array of parameterized types: pair<string>[] table=new pair<string>[10]//error, the erase mechanism will cause any pair< in the table array to be inserted Otherclass>
4.Varargs warning, passing an instance of a generic type to a variable-parameter method. Because the number variable argument is actually an array, it violates the third one, but only gets a warning.
5. Type variables cannot be instantiated: (if you want to construct objects by T)
New T (), t.class//both error
Public Pair () {first=new T (); second=new t ();} Error
saddly, because T.class cannot be called:
First=t.class.newinstance () is also wrong.
If you want to construct a generic object by calling Class.newinstance through reflection: Create a new generic method in pair, because the class itself is generic, String.class is an instance of class<string>
publicstatic <T> pair<t> Makepair (class<t> cl) { try{ Returnnew pair<>(Cl.newinstance (), cl.newinstance ());} Catch (Exception ex) {returnnull;} }
Call:
Pair<string> p = pair.makepair (String.class);
You cannot construct an array of type variables:
public static <T> t[] Minmax (t[] a) {t[] mm = new t[2]; ...} Error
6. Invalid type variable in static context of generic class
7. You cannot throw or capture an instance of a generic class:
Generic class cannot extend throwable; cannot catch type variable (can throws type variable in declaration)
。。。。
Inheritance rules for generic types:
There's no relationship between Pair<employee> and pair<manager>.
Pair<employee> b=pair<manager> a//error
Assuming that the conversion is possible, a low-level employee can be stored in B (because there is no protection)
Note the difference between arrays and generics, if a manager[] array is assigned to employee[] variable A, then this variable will have special protection, that is, low-level employees cannot store to a[0].
A subtype of the original type pair when pair<employee>
A generic class can extend or implement other generic classes. such as arraylist<t> implements the List<t> interface.
Wild-Letter wildcard types:
Java Learning Note 9.22 (generics)