1.1.the nature of parameterized types
Java parameterized types are fundamentally different from the parameterized types of C + + . The former is only a means of compiler type checking, while the latter is really a new type that is detrimental to the original type.
Public classGenericParameterTest3 {Static classhello{}Static classworld{}Static classGeneric<t>{} Public Static voidMain (string[] args) {//The three are compiled in exactly the same type. Generic G1=NewGeneric (); System.out.println ("Generic:" +G1.getclass ()); Generic<Hello> g2 =NewGeneric(); System.out.println ("GENERIC<HELLO>:" +G2.getclass ()); Generic<World> g3 =NewGeneric<world>(); System.out.println ("GENERIC<WORLD>:" +G3.getclass ()); //G1 equals G2, G1 equals G3System.out.println (G1.getclass ()==G2.getclass ()); System.out.println (G1.getclass ()==G3.getclass ()); //compilation error. //generic }
when run, it is found that the type of a generic type with different type parameters is exactly the same as the class type of the original type . No matter how many generic types are instantiated, only one class file is generated after compilation , and only one class object is loaded.
The results of the operation are as follows:
Generic:class Com.test.javatechnology.genericparameter.genericparametertest3$generic
Generic
Generic<world>:class Com.test.javatechnology.genericparameter.genericparametertest3$generic
True
True
The above-mentioned nature of the parameterized type of Java determines that a type conversion can be forced between parameterized types with different types of parameters.
Key Java Technologies (22) The nature of the parameterized type of generics