Schematic diagram of Java Virtual machine--a constant pool in 1.2.2, class file (top) [go]

Source: Internet
Author: User

NO1. Where is Chang in the class file?

My previous article "schematic diagram of Java Virtual machine" 1, class file basic organizational structure has mentioned the class file structure, in the class file, after the magic number, the sub-version number, the main version, followed by the constant pool of data area, such as the location of the red line include:

Once you know the location of the constant pool, let's uncover what's in the constant pool.

NO2. How is the inside of a constant pool organized?

The organization of a constant pool is simple, and the two-byte position of the front-end is called the constant pool counter (constant_pool_count), which records the number of constant pool entries (Cp_info) that are the constituent elements of constant pools. A constant_pool_count-1 constant Pool entry (Cp_info)is immediately followed. As shown in the following:

NO3. What is the structure of a constant pool entry (cp_info)?

Each constant pool entry (cp_info) corresponds to the literal of a type in the class file. Let's take a look at the structure of the constant pool entry (cp_info) :

The JVM virtual machine specifies different tag values and different types of literal correspondence as follows:

Therefore, depending on the tag values in the Cp_info, the cp_info can be refined to the following structure:

Constant_utf8_info,constant_integer_info,constant_float_info,constant_long_info,
Constant_double_info,constant_class_info,constant_string_info,constant_fieldref_info,
Constant_methodref_info,constant_interfacemethodref_info,constant_nameandtype_info,constant_methodhandle_info,
Constant_methodtype_info,constant_invokedynamic_info
.

Now let's look at the structure of the refined constant pool as it looks like:


NO4. can a constant pool represent that information?

NO5. constants for int and float data typesHow is it represented and stored in a constant pool? (Constant_integer_info, Constant_float_info)

The Java language Specification specifies that data types of type int and float type occupy 4 bytes of space. So how do the constants of that type exist in a class bytecode file stored? Accordingly, in a constant pool, constants of type int and float are represented by Constant_integer_info and Constant_float_info, respectively, with their structure as follows:

Example: Build the following class Intandfloattest.java, in which we declare five variables, but take the values of the two int types of 11fand float type .

[Java]View Plaincopyprint?
  1. Package COM.LOUIS.JVM;
  2. Public class Intandfloattest {
  3. private Final int a = 10;
  4. private Final int b = 10;
  5. private float c = 11f;
  6. private float d = 11f;
  7. private float e = 11f;
  8. }


Then compile the compiler into a intandfloattest.class bytecode file, we look at the information in the constant pool through the javap-v intandfloattest directive, and we can see that although we have written two times in the code 10 and three times 11f, but in a constant pool, there is only one constant and a constant of 11f, as shown in:

From the results you can see that the constant pool #8 constant Pool entry (cp_info) is Constant_integer_info, with a value of ten, and the #23 constant Pool entry (cp_ Info) is constant_float_info, with a value of 11f. (The other things in the constant pool don't get tangled up, we'll talk about it all at once.)

Any place in the code where int is used, the pointer value pointing to the constant pool is used #8 positioned to the #8 constant Pool entry (cp_info), which is a value of struct Constant_integer_info, and when used with a float type of 11f , it also points to the pointer value #23 of the constant pool to navigate to the #23 constant pool entry (Cp_info) as a value of 11f structural body constant_float_info. As shown in the following:

NO6. constants for long and double data typesHow is it represented and stored in a constant pool? (Constant_long_info, Constant_double_info)

The Java language Specification specifies that a long type and a double type of data type Occupy 8 bytes of space. So how do the constants of that type exist in a class bytecode file stored? Accordingly, in a constant pool, constants of type long and double are represented using Constant_long_info and Constant_double_info, respectively, and their structure is as follows:

Example: Build the following class Longanddoubletest.java, in which we declare six variables, but the values are two Long types of -6076574518398440533l and 10.1234567890Dof type Double.

[Java]View Plaincopyprint?
  1. Package COM.LOUIS.JVM;
  2. Public class Longanddoubletest {
  3. private long a = -6076574518398440533l;
  4. private long B = -6076574518398440533l;
  5. private long C = -6076574518398440533l;
  6. private Double d = 10.1234567890D;
  7. private Double e = 10.1234567890D;
  8. private Double f = 10.1234567890D;
  9. }


Then compile the compiler into a longanddoubletest.class bytecode file, and we'll look at the information in its constant pool through the javap-v longanddoubletest directive, and we can see that although we've written three times in the code -6076574518398440533l and three times 10.1234567890D, but in a constant pool, there is only one constant -6076574518398440533l and one constant 10.1234567890D, as shown in:

From the results you can see that the constant pool #18 constant Pool entry (cp_info) is constant_long_info, with a value of -6076574518398440533l The first #26 constant Pool entry (cp_info) is constant_double_infowith a value of 10.1234567890D. (The other things in the constant pool don't get tangled up, we'll talk about it all at once.)

All places in the code that use the long type -6076574518398440533l are positioned to the #18 constant Pool entry (cp_info) using pointer values pointing to the constant pool #18. that is, a struct with a value of -6076574518398440533l constant_long_info, and a double type of 10.1234567890D , it also points to the pointer value of the constant pool #26 to navigate to the #26 constant Pool entry (cp_info) , which is a struct with a value of 10.1234567890D Constant_double_info. As shown in the following:

NO7. string constants of type stringHow is it represented and stored in a constant pool? (Constant_string_info, Constant_utf8_info)

For strings, the JVM stores the literal of the string type in the UTF-8 encoded format in the class bytecode file. That might be a little confusing. North, we first from the intuitive Java source in the appearance of double quotation marks "" In the string, when the compiler compiles, the strings will be converted to Constant_string_info structure, and then placed in the constant pool. Its structure is as follows:

As shown in the struct, the value of String_index in theconstant_string_info struct points to the constant_utf8_info struct, and the UTF-8 encoded data of the string is in the struct. As shown in the following:

Consider the example of defining a simple Stringtest.java class, and then adding a "JVM principle" string to this class, and then we'll look at how it's organized in the class file.

[Java]View Plaincopyprint?
    1. package com.louis.jvm;  
    2.   
    3. public class  stringtest {  
    4.      Private string s1 =  "JVM principle";   
    5.     private string s2 =  "JVM principle";   
    6.      "JVM principle";   
    7.     private string s4 =  "JVM principle";   
    8. }  

After compiling the Java source code into a stringtest.class file, executing the javap-v stringtest command in the directory of this file, you will see the following contour of the constant pool information:

(PS: Use the javap-v instruction to see the information that is easy for us to read, to see real bytecode files can use Hexwin, notepad++, Utraedit and other tools. )

In the polygon diagram, we can see that the constant_string_info structure is located at the #15 index of the constant pool. The UTF-8 encoded byte array that holds the Java Virtual Machine principle string is placed in the constant_utf8_info struct, which is located at the #16 index of the constant pool. The above figure just looks at the outline, let's take a closer look at their organization. Please see:

By visible: The "JVM principle " of the UTF-8 encoded array is: 4A564D E5 8E 9fe7 90 86, and deposited in the constant_utf8_info structure.

NO8. Class names defined in the class file and classes used in the classHow are they organized and stored in a constant pool? (Constant_class_info)

The JVM encapsulates the fully qualified name of all classes used in a Java class into a constant_class_info struct in binary form , and then places it in a constant pool. The tag value of the constant_class_info is 7. the structure is as follows:

Tips: Fully qualified name and binary form of a class

In some Java source code, we will use a number of classes, such as we have defined a classtest class, and put it under the COM.LOUIS.JVM package, the Classtest class is fully qualified named Com.louis.jvm.ClassTest, after the JVM compiler compiles the class into a class file, this fully qualified name is stored in the class file in the fully qualified binary form, that is, it will put the full qualifier "." Replace with "/" , that is, the fully qualified name of the classtest class stored in the class file is "com/louis/jvm/classtest". Because the fully qualified name of this form is placed in a byte-code file in the class binary form, it is called a fully qualified name in the binary form.

For example, we define a very simple classtest class to see how a constant pool stores the fully qualified name of a class.

[Java]View Plaincopyprint?
    1. Package COM.JVM;
    2. Import Java.util.Date;
    3. Public class Classtest {
    4. private Date date;
    5. }

After compiling the Java source code into a classtest.class file, executing the javap-v classtest command in the directory of this file, you will see the following contour of the constant pool information:

As shown in the constant pool of the classtest.class file, there are 3 constant_class_info structures, each representing the class information used in classtest . Let's look at one of the expressions COM/JVM. the constant_class_info structure of the classtest. Its position in the constant pool is #1, and its name_index value is #2, which points to the 2 constant pool entries for the constant pool, as follows:

Schematic diagram of Java Virtual machine--a constant pool in 1.2.2, class file (top) [go]

Related Article

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.