JDK Learning Path---java generics

Source: Internet
Author: User

The benefit of 1:java generics is that type checking can be implemented, which is more secure, and can be extended and enhanced by functions.

2: The concept of generics is a parameterized type, equivalent to the formal parameters in the method, first make a placeholder and then in use when the real parameters are passed in.

3:

 Packagegeneric;/*** Custom generic class *@authorPC **/ Public classGenericmodel <T>{    PrivateT data;  PublicGenericmodel () {} PublicGenericmodel (T data) { This. data =data; }     Public voidsetData (T data) { This. data =data; }     PublicT GetData () {return  This. Data; }}
 Packagegeneric; Public classGenerictest { Public Static voidMain (String[]args) {Genericmodel<string>genericmodel =NewGenericmodel<string>(); Genericmodel.setdata ("11"); System.out.println ("String Data" +genericmodel.getdata ()); Genericmodel<integer>genericmodel1 =NewGenericmodel<integer>(); Genericmodel1.setdata (22); System.out.println ("Integer Data" +genericmodel.getdata ()); /*** found the output of the following two, this shows that at the time of real implementation, the class has only one type, and there are no two different types of classes * when using generic classes, although two type parameters, but actually in memory only one type of parameter * to investigate its cause, Is that the concept of generics in Java is intended to cause it to work only in the code compilation phase, and during compilation, when the generic results are correctly validated, the relevant information for generics is erased, * that is, the class file after the successful compilation is not a package containing any generic information.         Generic information is not entered into the run-time phase.         * * * Generic types are logically viewed as multiple different types, and are essentially the same basic types. * */System.out.println (Genericmodel.getclass ());    System.out.println (Genericmodel1.getclass ()); }}

JDK Learning Path---java 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.