Schematic diagram of Java Virtual Machine 1.4 field table set in the class file -- how the field is organized in the class file, graphic tutorial on Virtual Machine networking
0. Preface
Understanding the principles of JVM virtual machines is the only way for every Java programmer to practice. However, the JVM virtual machine has a lot of things that are widely described. Most of the tutorials or blogs about the JVM Virtual Machine principles are full of text descriptions, it is difficult to give people a visual perception. After reading it, I feel confused.
Thanks to the above, I plan to summarize what I learned in the process of learning JVM virtual machines into a series of examples, in the form of illustrations, based on my own understanding, I want to provide some help to Java programmers who want to understand the principles of Java virtual machines.
After reading this article, you will learn:
1,How is the field defined in the class organized in the class file?
2,How are different data types represented in the class file?
3,Initialization and assignment of field fields of the static final type
1. Overview
Field table setIs composed of severalField table (field_info). For several fields defined in the classJVMCompileClassAfter the file, the corresponding field information is organized into a structure called the field table set. The field table set is a class array structure, as shown in:
Note: The fields mentioned here refer to static or non-static variables defined in the class, rather than variables defined in methods in the class. Note the differences.
For example, if a class defines five fields,JVMWhen this class is compiled, fiveField table (field_info)Information, set the value of the field counter in the field table set to 5, and place the information of the five field tables following the field counter in sequence.
2. Position of the field table set in the class file
Field table setKeep upClassFileInterface index setSee the following figure:
3. What information should a Field in Java contain? ------ Field table field_info struct Definition
For the above field representation,JVMSpecifications of virtual machinesField_infoStruct to describe the field. Its representation is as follows:
I will explain it one by one.FIeld_infoElements:Access flag (access_flags),Name index (name_index),Descriptor_index),Attribute Table set
4. Access flag of field
As shown inField_infoStruct,FieldFieldAccess flag (access_flags)It occupies two bytes and can express the following information:
For example, if we have a definition in a classFieldField: private static String str;, then on the access flag15BitACC_PRIVATEAnd13BitACC_STATICThe flag space must be 1.FieldDomainStrThe access flag information of is as follows:
As shown in,StrFieldAccess flagIs0x000A, Which consists of two ModifiersACC_PRIVATEAndACC_STATIC.
Based on the givenAccess flag (access_flags), We can use the following operations to get the modifiers for this domain:
The access flag value of the str field listed above is000A, Then the feature value of the above sign is obtained respectively. The result is1OnlyACC_PRIVATEAndACC_STATIC, So the flag of this field only hasACC_PRIVATEAndACC_STATIC.
5. Field Data Type and field name
ClassShows the data type representation of the file:
FieldField name.Private static String strOfFieldField, where"Str"Is the name of this field.
ClassFile Name andFieldThe data type of the field indicates that it is stored as a string in the constant pool. InField_infoIn the struct, the access flag is the field name index and the field descriptor index, which occupy two bytes respectively, and the internal storage is the index pointing to a constant pool item in the constant pool, the string stored in the corresponding constant pool item, indicating the name and descriptor of the field respectively.
6. Attribute Table set ----- static field Initialization
In the definitionFieldDuring the field processFieldDirectly assign values to fields, as shown below:
public static final int MAX=100;public int count=0;
For virtual machines
FieldThe timing of field assignment is different:
- For non-static (I .e. noneStaticModified)FieldThe field value is displayed in the instance constructor.<Init> ()Medium
- For staticFieldField, there are two options: 1. In the static Constructor<Cinit> ()2. UseConstantValueAttribute assignment
Sun javac compiler for static
FieldField initialization and value assignment policy
The current Sun javac compiler is:FinalAndStaticModifyFieldAnd the field is of the basic type orStringType, the compiler willField_infoAddConstantValueType struct, which is used when assigning valuesConstantValueAssign values.FieldThe field is notFinalModifier, or is not a basic type orStringType.<Cinit> ().
ForPublic static final init MAX = 100;The javac compiler is compiling thisFieldField ConstructionField_infoIn addition to the access flag, name index, and descriptor index,ConstantValueType Attribute Table.
7. instance resolution:
Definition is as follows:SimpleClass, and then viewSimple. classFile Content and integrationJavap-v SimpleContent of the generated constant pool for analysisStr fieldField structure:
package com.louis.jvm;public class Simple {private transient static final String str ="This is a test";}
Note:
1.Field counterThe value isZero x 0001Indicates that this class definesField
2.Field access flagYes0x009A, Binary is00000000 10011010, That is,9, 12, 13, 15The flag is1, The identifier of this field is:ACC_TRANSIENT, ACC_FINAL, ACC_STATIC, and ACC_PRIVATE;
3.Name IndexThe value isZero x 0005, Pointing to5Item, which is"StrIndicates thisFieldThe name isStr;
4.Description IndexThe value isZero x 0006, Pointing to6Item, which is"Ljava/lang/String;", Indicates thisFieldThe data type of isJava. lang. StringType;
5.Attribute Table countersThe value is0x0001,IndicatesField_infoThere is also an Attribute Table;
6.Attribute Table name indexThe value isZero x 0007, Pointing to7Item, which is"ConstantValueIndicates that the name of this attribute table isConstantValueThat is, the Attribute Table type isConstantValueType;
7. The value in the attribute length isZero x 0002Because this attribute table isConstantValueType. Its value is fixed2;
8. The value in the constant value index isZero x 0008, Pointing to8Item, which isCONSTANT_String_infoType, indicating"This is a test. In thisFieldThis constant pair is used when values are assigned.FieldAssign values.
Author's words
This article is one of the series of "Schematic diagram of Java virtual machines". If you are interested, please follow other articles in this series ~
I think this article is good. Just like it ~~ Your encouragement is a powerful motivation for me to continue sharing my knowledge!
Certificate -----------------------------------------------------------------------------------------------------------------------------------------
This article is from http://blog.csdn.net/luanlouis/. if you need to download it, please note it. Thank you!