This article continues with the above, for generic programming, from the following aspects to explain:
- Why generic programming is used
- Defining a simple generic class
- Generic methods
- Qualification of type variables
- Generic code and virtual machines
- Constraints and limitations
- Inheritance rules for generic types
- Wild-Letter wildcard type
- Reflection and Generics
4. Generic Code and virtual machine
Here the content is more and miscellaneous, we first summarize several main ideas:
- There are no generics in the virtual machine, only ordinary classes and methods
- All of the type parameters are converted with their qualified type
- The bridge method is synthesized to maintain polymorphism
- To preserve type safety, insert a forced type conversion if necessary
(1) When a generic type is defined, the system automatically provides a corresponding primitive type. The name of the original type is the generic type name after the type parameter is deleted. Erases the type variable and replaces it with a qualified type.
(2) When a program invokes a generic method, if the return type is erased, the compiler inserts the corresponding coercion type conversion.
5. Constraints and Limitations
The following is an in-depth discussion of some of the limitations that you need to consider when using Java generics, most of which are caused by type erasure
5.1 cannot instantiate a type parameter with a base type
You cannot replace a primitive type with a type parameter, that is, there is no Pair<double>
only Pair<Double>
. This is because after the type erasure, the pair class contains a field of type object, and object cannot store a double value.
5.2 Run-time type queries are only available for the original type
Objects in a virtual machine always have a specific non-generic type, so all type queries produce only the original type.
5.3 Cannot create an array of parameterized types 5.4 varargs warning 5.5 cannot instantiate a type variable
Cannot use like new T (...), new t[] Or a type variable in an expression such as t.class.
5.6 Invalid type variable in the static context of a generic class 5.7 cannot throw or catch instances of a generic class 5.8 note conflicts after erasing 6, inheritance rules for generic types
In simple terms, it is not the relationship between S and T, usually Pair<S>
and Pair<T>
not. For example, the manager class is a subclass of the employee class, but it has nothing to Pair<Employee>
do with it Pair<Manager>
.
7. Wildcard Type
Fixed generic type systems are uncomfortable to use, so a wildcard type appears. For example
Pair<? extends Employee>
Represents any generic pair type whose type parameter is a subclass of employee, as Pair<Manager>
opposed to a Pair<String>
.
7.1 Wildcard characters for super-type qualification
Wildcard qualification is very similar to a type variable qualification, but there is also an additional function, that is, you can specify a super-type qualification. For example
superManager
This wildcard is limited to all of the manager's hyper-types.
7.2 Unqualified wildcard characters
Unqualified wildcard usage, such as Pair<?>
.
7.3 Wildcard Capture
Wildcard captures are only legal if there are many restrictions, and the compiler must be able to be confident that the wildcard expression is a single, deterministic type.
Below, we use a routine to review the concepts that we have described earlier
Package pair3; Public classpairtest3{ Public Static void Main(string[] args) {Manager CEO =NewManager ("Gus greedy.",800000,2003, A, the); Manager CFO =NewManager ("Sid Sneaky",600000,2003, A, the); Pair<manager> buddies =NewPair<> (CEO, CFO); Printbuddies (buddies); Ceo.setbonus (1000000); Cfo.setbonus (500000); Manager[] Managers = {CEO, CFO}; pair<employee> result =NewPair<> (); Minmaxbonus (managers, result); System. out. println ("First:"+ Result.getfirst (). GetName () +", Second:"+ Result.getsecond (). GetName ()); Maxminbonus (managers, result); System. out. println ("First:"+ Result.getfirst (). GetName () +", Second:"+ Result.getsecond (). GetName ()); } Public Static void printbuddies(pair<? extends employee> P) {Employee first = P.getfirst (); Employee second = P.getsecond (); System. out. println (First.getname () +"and"+ second.getname () +"is buddies."); } Public Static void Minmaxbonus(manager[] A, pair<? Super manager> result) {if(A = =NULL|| A.length = =0)return; Manager min = a[0]; Manager max = a[0]; for(inti =1; i < a.length; i++) {if(Min.getbonus () > A[i].getbonus ()) min = A[i];if(Max.getbonus () < A[i].getbonus ()) max = A[i]; } result.setfirst (min); Result.setsecond (max); } Public Static void Maxminbonus(manager[] A, pair<? Super manager> result) {Minmaxbonus (A, result); Pairalg.swaphelper (result);//Ok--swaphelper captures wildcard type}}class pairalg{ Public StaticBooleanHasnulls(Pair<?> p) {returnP.getfirst () = =NULL|| P.getsecond () = =NULL; } Public Static void Swap(Pair<?> p) {Swaphelper (P);} Public Static<T>void Swaphelper(Pair<t> p) {T t = P.getfirst (); P.setfirst (P.getsecond ()); P.setsecond (t); }}
Java Core Technology (v)--generic programming (2)