Java Generic Implementation Principles

Source: Internet
Author: User

The idea of generics was first created in the C + + language template (Templates), which was later borrowed from Java. Although the ideas are consistent, they are inherently different.

Templates in C + + are really generic, compiling different template type parameters to correspond to different target code,list<integer> and list<string> are two different types, which are called true generics.

This generic implementation causes the type to swell because different classes are generated for different specific parameters.

Although List<integer> and list<string> in Java are different classes in the source code, they are replaced with the original type in the compiled bytecode (list<object >), so at run time,list<integer> and list<string> are the same class.

Generics in Java are a special kind of syntax, implemented by type Erasure, which is called pseudo-generics.

type erasure , which refers to associating a generic type instance to the same byte-code. The compiler generates only one byte code for a generic type and associates its instance to this bytecode.

The key to type erasure is to clear the information about the type parameter from the generic type and, if necessary, to add methods for type checking and type conversion.

        New Arraylist<>();        Intlist.add (3);        List<String> strlist  new arraylist<>();        Strlist.add ("Hello");         = = Strlist.getclass ());  // output is True

Intlist and strlist all belong to the same class.

Create an object that can store only integers ArrayList , after the add an integer value, call add an ASD string with reflection, and run the add(Object o) code without error, and the result prints 1 and two values of ASD. In this case add(Integer o) , the codenosuchmethodexception exception is thrown by the runtime using the reflection call method. This fully proves that after compiling, the generic information of the integer is erased and only the original type is preserved.

Create a List<integer> object intlist, call its Add () method with reflection, add a string class element to the intlist, and run the code without error.

        New Arraylist<>();        Intlist.add (3);        Intlist.getclass (). GetMethod ("Add", Object. class). Invoke (Intlist, "Hello");          for (int i = 0; i < intlist.size (); i++) {            System.out.println (Intlist.get (i));         }         // The output result        is // 3         // Hello

This shows that after compiling, the generic information of the integer is erased, and the intlist is the original type list<object>.

Modify Code GetMethod ("Add", Object.class), change to GetMethod ("add", Integer.class)

Intlist.getclass (). GetMethod ("Add", Integer.                Class). Invoke (Intlist, "Hello");

Run an error, Intlist class list<integer> does not ("add", Integer.class) method, only ("Add", Object.class).

Automatic type conversion

Generics in Java, in addition to type erasure, automatically generate checkcast directives to enforce type conversions.

        New Arraylist<>();        Intlist.add (3);         int a = intlist.get (0);

An object of type integer is returned using the Get method of Intlist.

Mark

Java Generic Implementation Principles

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.