Constraints and limitations of Java generics
@author Ixenos
- 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, only tests whether a is a pair of any type, You see the compiler error warning if (a instanceof pair<t>) // pair <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:
New // ERROR
-
-
- by compiler type is erased, the type of table is pair[], which can be a covariant group of object[], for example://Note that this pair is static type, not a generic parameter, so pair[]
-
object[] Objarray =
Table;
The - array remembers his element type pair, if you attempt to store other types of elements , an exception is thrown ( array store check ), for example:
-
objarray[0] = "Hello"; //
error--component type is Pair
- however, for generic type pair<string>, type erasure invalidates this different class-checking mechanism, for example:
-
objarray[0] = new pair<employee> ();
The
- array store only checks the type of erasure, and because the Java language Design array can be covariant , it can be checked through the array store by compiling
- , but it will still result in a type error, so creating a parameter is not allowed Array of type
- Note that variables that declare type pair<string>[] are legal, but they cannot be created (directly initialized with new PAIR<STRING>[10]
-
- indirect implementation of a generic array: The generic array wrapper, such as the ArrayList class, maintains an object array and then indirectly implements the generic array by qualifying the type with the import and export method set, get, and the cast array type, for example: ArrayList: Arraylist<pair<t>>, arraylist<t>
- cannot instantiate type variable 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
- However, when designing a generic array wrapper , such as method Minmax returns a t[] array, the generic array wrapper cannot be cast, because the type erase, return (T []) new object is meaningless and strong to go. At this point, we have to use reflection, call Array.newinstance:
-
-
- "API Document description" PUBLIC&NBSP;CLASS<?>&NBSP; getcomponenttype () returns the that represents the array component type;
Class
. If this class does not represent an array class, this method returns NULL.
-
-
-
- and the implementation of the ToArray method in the ArrayList class is troublesome.
- Public object[] ToArray () no parameter, return object[] Array
Public 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, but my contents: return(t[]) arrays.copyof (elementdata, size, A.getclass ()),//a.getclass () runtime type of the run-time destination array System.arraycop Y (Elementdata,0, A, 0, size); if(A.length >size) A[size]=NULL; returnA; }
- Invalid type variable in static context of generic class
- cannot throws or catch instances of generic classes (for exceptions)
-
- Notice the conflict after erasing
Constraints and limitations of Java generics