Key Java Technologies (22) The nature of the parameterized type of generics

Source: Internet
Author: User

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&LT;HELLO&GT;:" +G2.getclass ()); Generic<World> g3 =NewGeneric<world>(); System.out.println ("GENERIC&LT;WORLD&GT;:" +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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.