Java Class file format (4)

Source: Internet
Author: User


Past Review
In the previous blog, I deeply understood the Java Class file format (3). I introduced two types of data items in the constant pool: CONSTANT_Utf8_info and CONSTANT_NameAndType_info. CONSTANT_Utf8_info stores almost all types of strings, including method names, field names, descriptors, and so on. CONSTANT_NameAndType_info is a method symbol reference or a part of the field symbol reference. That is to say, if a method is called in the source file or a field is referenced (whether it is a method or field in this class, or reference methods and fields in other classes), The CONSTANT_NameAndType_info corresponding to this method or field will appear in the constant pool. Note: only when a method or field is referenced, The CONSTANT_NameAndType_info corresponding to it exists in the constant pool. If only one field is defined in the current class and it is not accessed, or if you define a method without calling it, the corresponding CONSTANT_NameAndType_info data item will not appear in the constant pool. CONSTANT_NameAndType_info references two CONSTANT_Utf8_info, one is name_index, the storage method name or field name, and the other is descriptor_index, which stores the method descriptor or field descriptor.
For details about the two constant pool data items, refer to the previous blog: a deep understanding of the Java Class file format (3 ). The previous articles on Virtual Machine and class file formats have been included in my blog column. If you want to fully understand this knowledge, read the blog in my column in order, column address: http://blog.csdn.net/column/details/zhangjg-java-blog.html. Other articles about understanding java in the future will also be included in this column. You are welcome to pay attention and exchange.
Next we will continue to explain in detail other types of data items in the constant pool. This article is followed by the previous articles. We recommend that you read the previous articles.

Description of various data item types in the constant pool (continued)
(3) CONSTANT_Integer_info
The CONSTANT_Integer_info data item in a constant pool can be considered as an instance of the CONSTANT_Integer type. It stores the int type data values in the source file. Similarly, as a data type in the constant pool, its first byte is also a tag value, and its tag value is 3, that is, when the VM reads a data item with a tag value of 3, it knows that the data item is a CONSTANT_Integer_info, which stores the value of the int type. The four bytes next to the tag are called bytes, which is the integer value of the int type value. Its memory layout is as follows:

The following is an example of code. The sample code is as follows:

package com.jg.zhang;public class TestInt {void printInt(){System.out.println(65535);}}

Decompile the class file generated by the above class:
D:\Workspace\AndroidWorkspace\BlogTest\bin>javap -v -c -classpath . com.jg.zhang.TestInt

The Decompilation results are listed below. Because of the long decompilation results, most of the information is omitted:
  ..................  ..................Constant pool:   ..................   ..................  #21 = Integer            65535   ..................   ..................{     ..................     ..................  void printInt();    flags:    Code:      stack=2, locals=1, args_size=1         0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;         3: ldc           #21                 // int 65535         5: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V         8: return      LineNumberTable:        line 6: 0        line 7: 8      LocalVariableTable:        Start  Length  Slot  Name   Signature               0       9     0  this   Lcom/jg/zhang/TestInt;}

In the above output results, The Decompilation results of the printInt method are retained, and 21st items in the constant pool are retained. First, let's take a look at the bytecode command with the index 3 in the result of the printInt method decompilation:
3: ldc           #21                 // int 65535

This ldc command references 21st items in the constant pool, and 21st items are a CONSTANT_Integer_info, And the integer value stored in this CONSTANT_Integer_info is 65535.

(4) CONSTANT_Float_info
The CONSTANT_Float_info data item in a constant pool can be considered as an instance of the CONSTANT_Float type. It stores the float data values in the source file. Similarly, as a data type in the constant pool, its first byte is also a tag value, and its tag value is 4, that is, when the VM reads a data item with a tag value of 4, it knows that the data item is a CONSTANT_Float_info and that it stores a float value. The four bytes next to the tag are called bytes, which is a float value. Its memory layout is as follows:

For example, if a code in the source file uses a float value, as shown below:
void printFloat(){System.out.println(1234.5f);}

A CONSTANT_Float_info corresponds to the constant pool of this class. The CONSTANT_Float_info format is as follows:


The Code decompilation result is as follows:
Constant pool:..........................  #29 = Float              1234.5f........................{........................  void printFloat();    flags:    Code:      stack=2, locals=1, args_size=1         0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;         3: ldc           #29                 // float 1234.5f         5: invokevirtual #30                 // Method java/io/PrintStream.println:(F)V         8: return      LineNumberTable:        line 10: 0        line 11: 8      LocalVariableTable:        Start  Length  Slot  Name   Signature               0       9     0  this   Lcom/jg/zhang/TestInt;}


(5) CONSTANT_Long_info
The CONSTANT_Long_info data item in a constant pool can be considered as an instance of the CONSTANT_Long type. It stores the value of long data in the source file. Similarly, as a data type in the constant pool, its first byte is also a tag value, and its tag value is 5, that is, when the VM reads a data item with a tag value of 5, it knows that the data item is a CONSTANT_Long_info and that it stores a long value. The following eight bytes next to the tag are called bytes, which is a long value. Its memory layout is as follows:

For example, if a code in the source file uses a long value, as shown below:
void printLong(){System.out.println(123456L);}

A CONSTANT_Long_info corresponds to the constant pool of this class. The CONSTANT_Long_info format is as follows:

Code decompilation result:
Constant pool:............................  #21 = Long               123456l............................{............................  void printLong();    flags:    Code:      stack=3, locals=1, args_size=1         0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;         3: ldc2_w        #21                 // long 123456l         6: invokevirtual #23                 // Method java/io/PrintStream.println:(J)V         9: return      LineNumberTable:        line 7: 0        line 8: 9      LocalVariableTable:        Start  Length  Slot  Name   Signature               0      10     0  this   Lcom/jg/zhang/TestInt;}


(6) CONSTANT_Double_info
The CONSTANT_Double_info data item in a constant pool can be considered as an instance of the CONSTANT_Double type. It stores the values of double data in the source file. Similarly, as a data type in the constant pool, its first byte is also a tag value, and its tag value is 6, that is, when the VM reads a data item with a tag value of 6, it knows that the data item is a CONSTANT_Double_info and that it stores a double value. The following eight bytes next to the tag are called bytes, which is a double value. Its memory layout is as follows:

For example, if a code in the source file uses a double-type value, as shown below:
void printDouble(){System.out.println(123456D);}


A CONSTANT_Double_info corresponds to the constant pool of this class. The CONSTANT_Double_info format is as follows:

Code decompilation result:
Constant pool:............................  #21 = Double             123456.0d............................{............................  void printDouble();    flags:    Code:      stack=3, locals=1, args_size=1         0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;         3: ldc2_w        #21                 // double 123456.0d         6: invokevirtual #23                 // Method java/io/PrintStream.println:(D)V         9: return      LineNumberTable:        line 7: 0        line 8: 9      LocalVariableTable:        Start  Length  Slot  Name   Signature               0      10     0  this   Lcom/jg/zhang/TestInt;}


(7) CONSTANT_String_info
In the constant pool, a CONSTANT_String_info data item is an instance of the CONSTANT_String type. It stores text strings and can be viewed as a string object that exists in the class file. Similarly, its first byte is the tag value with a value of 8. That is to say, when the Virtual Machine accesses a data item and judges that the tag value is 8, it means that the accessed data item is a CONSTANT_String_info. The last two bytes next to the tag are referenced by a constant pool called string_index, which points to a CONSTANT_Utf8_info. This CONSTANT_Utf8_info stores the string literal. Its memory layout is as follows:


For example, if a code in the source file uses a String constant, as shown below:
void printStrng(){System.out.println("abcdef");}

A CONSTANT_String_info corresponds to the constant pool of this class. The Decompilation result is as follows:
Constant pool:............................    #21 = String             #22            //  abcdef  #22 = Utf8               abcdef............................{............................  void printStrng();    flags:    Code:      stack=2, locals=1, args_size=1         0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;         3: ldc           #21                 // String abcdef         5: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V         8: return      LineNumberTable:        line 7: 0        line 8: 8      LocalVariableTable:        Start  Length  Slot  Name   Signature               0       9     0  this   Lcom/jg/zhang/TestInt;}

In the printString method, the byte code command ldc with a 3 index references 21st items in the constant pool, and the 21st items are a CONSTANT_String_info. The CONSTANT_String_info in the 21st item references 22nd items in the constant pool, the first item is a CONSTANT_Utf8_info. The string stored in CONSTANT_Utf8_info is abcdef. The memory layout of the reference relationship is as follows:



Summary
This article ends. Finally, this article describes the five data items in the constant pool: CONSTANT_Integer_info, CONSTANT_Float_info, CONSTANT_Long_info, CONSTANT_Double_info, and CONSTANT_String_info. These constant pool data items are directly stored as constant values, rather than symbolic references. The concept of symbolic reference is introduced again here, which will be explained in detail in the next blog, because the remaining four constant pool data items to be introduced in the next blog are symbol references, the four data items that indicate symbolic references directly or indirectly reference CONSTANT_NameAndType_info and CONSTANT_Utf8_info described in the previous article. Therefore, CONSTANT_NameAndType_info is part of the symbolic reference.
We can also know from this article. Although CONSTANT_String_info is a data item that directly stores values, CONSTANT_String_info is a bit special because it does not store strings directly, but references a CONSTANT_Utf8_info, which stores strings in the referenced CONSTANT_Utf8_info.
Finally, list the remaining four constant pool data items to be introduced in the next blog: CONSTANT_Class_infoCONSTANT_Fieldref_infoCONSTANT_Methodref_infoCONSTANT_InterfaceMethodref_info


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.