General type of the core foundation of Java 5 (3)-acting on the compilation stage-how to pass a String object to an Integer-type generic object?

Source: Internet
Author: User

Generics Act on the compilation phase:

Generics are used in the compilation phase and control the type during the compilation phase to ensure that only the specified type of data can be imported to the generic collection object during code writing. How to verify it? paste the following code:

Package highBasic. generic;

Import java. lang. reflect. InvocationTargetException; import java. lang. reflect. Method; import java. util. ArrayList;

Public class StringAddToGenericInteger {

Public static void main (String [] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {ArrayList Collection = new ArrayList (); ArrayList Collection2 = new ArrayList (); ArrayList Collection3 = new ArrayList (); // If you want to wait, collection2 and collection3 point to the same bytecode. System. out. println (collection2.getClass () = collection3.getClass (); // The printed result is true // ------- can I add a String object to collection3 through the compiler? // Collection3.add ("abc"); ------ error Method = collection3.getClass (). getMethod ("add", Object. class); method. invoke (collection3, "abc"); System. out. println ("transmits the String object to the Integer collection object through the compiler, and the output result is:" + collection3.get (0); // output: "Through the compiler, upload the String object to the set object of the Integer type. The output result is "abc" // describes the above implementation. // Then the question is, what is the purpose of this practice ----------------------------? }

}

Output statement in the above code:

System. out. println (collection2.getClass () = collection3.getClass ());

The output is true, indicating that collection2 and collection3 point to the same bytecode, although the types specified in the declaration stage are String and Integer, respectively.

This means that, after the java source program is compiled into a class file, the jvm does not know the type you have defined when the jvm runs the program. This further verifies that the generic type limitation mentioned above is applied during compilation and does not work during runtime.

Now that we understand this, we can imagine how to pass in the Set object to the non-qualified type value under the type limitation through the compilation period. So let's take a look at the following question. The solution has been implemented in the above Code.

How to pass a String object to an Integer-type generic object?

Method method = collection3.getClass (). getMethod ("add", Object. class );

With this core code, we can add any type of data to this set object.

Why?

In fact, it is very simple, because after the. java source files with limited types are compiled into a class, there is no type limitation. It can be understood that the generic type limitation is used during compilation. Once the class file is successfully compiled, the type limitation will no longer exist.

Through the reflection method above, we get exactly the information of the class file, and then get the add method of the generic set type through the getMethod () method of reflection. This method is equivalent to passing the String object to the generic set object of the Chinese Integer type limitation in the source program through the compilation phase.

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.