Ix. type information in Java

Source: Internet
Author: User

First, what is type information?

Simply understood, "type information" is a mechanism that is implemented in Java by discovering and using "class" information during program operation.

Java type information consists of two main components: Rtti and reflection.

Second, what is Rtti?

RTTI, the full name is run-time type information, the meaning of which is to recognize the type of an object at run time.

1.Class objects

In Java, a special object class is used to perform its rtti (this is an idea that uses a class to represent all types of objects, to distinguish which type is by the attributes in the specific class, and to use similar ideas in spring. All the beans in spring are represented by Beandefinition.

All classes are dynamically loaded into the JVM when they are first used, and the ClassLoader first checks to see if the class object is already loaded, and if it is not already loaded, the default ClassLoader will look for the. class file based on the class name; It is used to create all objects of this class.

Take a look at two examples to understand the process of downloading:

1  Public classClasstest {2     Static {3System. out. println ("classtest");4     }5      Public Static voidMain (String[]args) {6         7     }8 }9 Ten classCandy { One     Static { ASystem. out. println ("Candy"); -     } -}

The results are as follows:

In the Classtest class, when the main method is executed, the Classtest class is loaded automatically, and the class has static method block static, so the classtest is displayed;

In the Candy class, the candy class is not loaded because it has not been used for its elements.

Look again:

1  Public classClasstest {2      Public Static voidMain (String[]args) {3System. out. println (candy.i);4     }5 }6 7 classCandy {8      Public StaticFinalintI=1;//Define a constant9     Ten     Static { OneSystem. out. println ("Candy"); A     } -}

The results are as follows:

In this example, constants are determined during compilation, so calling Candy.i in the main function does not actually need to load the Classtest type information, so there is no execution of the statement in the static block.

2. The literal of the class

1  Public classClasstest {2      Public Static voidMain (String[]args) {3System. out. println (boolean.type);4System. out. println (Boolean.class);5System. out. println (Boolean.class);6         7     }8}

The results are as follows:

Ix. type information in Java

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.