Type parameters cannot be instantiated with a base type
You cannot replace a base type with a type parameter: for example, there is no pair<double>, only pair<double> The reason is the type erase. After erasing, the pair class contains a field of type Object , and object cannot store a double value. This embodies the independent state of the basic types in the Java language.
Run-time type queries are only available for original types (raw type)
runtime : Usually refers to the time the JVM executes after ClassLoader is loaded
type query : instanceof, GetClass, coercion type conversion
Primitive Type : that is (raw type), the generic type is erased by the compiler type as a qualified type of object or generic parameter (for example, Pair<t extends Comparable> Comparable is the limited type of T, the original type of the generic is comparable, so the pair class without generics is pair<comparable>), that is, the pair class contains the comparable type of domain
There is no generic type in the JVM
eg
if (a instanceof pair<string>) //error, test only if a is any type of pair, you will see the compiler ERROR warning if (A Span style= "color: #0000ff;" >instanceof pair<t>) //errorpair<string> p = (pair<string>) A; //warning, test only if a is a pair pair<string> stringpair = ...; pair<employee> employeepair = ...; if (stringpair.getclass () = = Employeepair.getclass ()) will get true because two calls to GetClass will return Pair.class
Cannot create an array of parameterized types (generic array)
array of parameterized types : refers to an array of types with generic parameters, also known as generic arrays, such as pair<t>[], t[]
You cannot instantiate an array of parameterized types, for example:
ERROR
Here we assume that we can instantiate, then after the compiler type is erased, the type of table is pair[], and we'll make it covariant to object[]:
object[] Objarray = table;
In general, the array remembers his element type pair, and if we try to store other types of elements , we throw an exception ( array store check ), for example:
error--component type is Pair
However, for generic type pair<string>, a type wipe invalidates this different class-checking mechanism, which is why you cannot instantiate a generic array !
New Pair<employee> (); //If the generic mechanism allows us to instantiate an array, there is no reason for this step to go wrong! And that goes against our original purpose. (limited type)
- The array store only checks the erased type , and because the Java language Design array can be covariant , you can compile
- Ability to check through array storage , but still result in a type error, so it is not allowed to create an array of parameterized types
- Note that variables that declare type pair<string>[] are legitimate, but they cannot be created (we should use new pair<string>[10]{directly ...} To initialize this variable)
indirect implementations of generic arrays :
Through the generic array wrapper, such as the ArrayList class, maintains an object array, then restricts the type through the import and export method set, get, and casts the array type, thereby indirectly implementing the generic array,
such as:arraylist:arraylist<pair<t>>, arraylist<t>
Cannot instantiate type variable t
- That is, you cannot use a type variable in an expression such as new T (..), New t[.], or T.class
- For example: Public Pair () {first = new T ();}//error! type erase changes t to object, calling the non-intended new object ()
- cannot use new T (..)
-
- Note: Class classes themselves are generic. String.class is an example of a class<string> , so the Makepair method can infer the type of pair
- cannot use new t[...]
- That is, you cannot (directly) create a generic array, refer to my other blog post (http://www.cnblogs.com/ixenos/p/5648519.html), and do not repeat
- solution: using generic array wrapper , for example ArrayList
-
- and the implementation of the ToArray method in the ArrayList class is troublesome.
- public object[] ToArray () No parameter, return to object[] array
public< Span style= "color: #000000;" > object[] ToArray () {return arrays.copyof (elementdata , size); }
- Public <T> t[] ToArray (t[] a)
a
-to store the t[] array of the list element (if it is large enough) otherwise assign a new array with the same run-time type , return the t[] array
@SuppressWarnings ("Unchecked" ) public <T> t[] ToArray (t[] a) {if (a.length < size) // make a new array of a ' s runtime type, BU T my contents: return (t[]) arrays.copyof (Elementdata, Size, A.getclass ()); A.getclass () The run-time type of the run-time destination array system.arraycopy (elementdata, 0, a, 0if (a.length > size) A[size] = nullreturn A;}
Invalid type variable in static context of generic class
Cannot throws or catch instances of generic classes (for exceptions)
Notice the conflict after erasing
- There is a principle in the Java Generics specification: "To support erase conversions, you need to force a restriction on a generic class or a type variable T to be a subclass of two interface types at the same time , which are different parameterization of the unified interface "
Generic constraints and restrictions for Java