Deep understanding of Java Virtual Machine 04--class structure files

Source: Internet
Author: User
Tags field table instance method

I. Program storage format

    • Unified Program Storage Format: virtual machines on different platforms are uniformly using the program storage format-byte code (bytecode) in all platforms;
    • The Java Virtual machine does not care about the source of the class file, but only the "class file" binary file format, which means that the Java virtual machine only know "class" file;
    • The Java compiler can compile the Java program code into the class file required by the virtual machine;

Two. Class file structure

The class file is a binary stream in 8 bytes, in a compact arrangement with no gaps in the middle; if you want to see a Class file, you can also pass Javap-verbose XXX if you see a byte code in addition to the WinHex compiler. Class output bytecode content, so it looks more intuitive.
1. Basic type
Unsigned number:

    • Definition: The basic data type, U1, U8 represents 1 bytes, 8 bytes.
    • Use: A string that can be used to describe a number, index, reference, utf-8 format;

Table:

    • Definition: A compound data type consisting of multiple unsigned numbers and other tables, usually ending with "_info".
    • Use: Describe a hierarchical relationship of composite structure data;

2. Magic number and version

    • Magic Number: The first 4 bytes of each class file, the only function is to determine whether the file can be accepted by a virtual machine class file;
    • Minor version number: the 5th and 6th bytes immediately following the magic number;
    • Major Version number: the 7th and 8th bytes represent the major version number, for example, 50 corresponds to JDK1.6.
    • You can use the hex compiler Winhex to open a class file.

3, Chang
The version number is followed by a constant pool entry, which can be understood as the resource repository in the class file;

    • Constant pool capacity counter: The U2 type, which represents the N-1 constant for this class file (since it is starting from 1 technology);
    1. 0 constants: Do not reference any one of the constant pool items
    • Constant Pool Place content each item is a table, mainly divided into two categories
    1. Literal: text string, final constant value, etc.;
    2. Symbol reference
      • Fully qualified names for classes and interfaces
      • Name and descriptor of the field
      • The name and descriptor of the method
    • Length
    1. Definition: UTF-8 encoded string length is the number of bytes;
    2. 65535 restrictions: Class file methods, fields and so on need to reference Constant_ utf8_ info type constant length is the U2 type, The maximum is 65535. If a variable or method name exceeds 64K, then this length will not fit, of course, it cannot be compiled.

4. Access Mark (access_flags)

    • The Chang is followed by the access flag. It mainly includes the class or interface, is not public, is not an abstract type, is not the final type.

5. Class index, parent class index, and Interface class collection

    • Java.lang.Object class index is 0;
    • The index of the class actually describes the relationship between the extends and implements of this class.

6. Field table collection (Field_info)

    • Variables used to describe the definition, including the access flag (Access_ flags), the name Index (name_ index), the description Index (DESCRIPTOR_ index), and the property sheet collection (attributes).
    • The information described is as follows:
      1. Scope: Public, private, protect
      2. Instance or class variable: static
      3. Variability: Final
      4. Concurrency Visibility: volatile
      5. Serializable: Transient
      6. field data type: base type, object, array, etc.
      7. Field name;
    • Field table Collection principles
      1. 1, does not list the superclass or parent class or the parent interface inherits the field;
      2. 2, it is possible to list the original Java code does not exist in the field (the inner class will automatically add to the external class instance of the field to refer to the external Class);
      3. 3, the Java language field is not overloaded;

7. Collection of method tables
Similar to the Set of field tables, a collection of method tables is used to describe the methods in a class file, but the Access flags and attribute table collections and Field table collections differ;

    • Access Flags:
      • Volatile, transient keywords can not be modified method, the method table is less of these two flags;
      • Synchronized, native, strictfp and abstract can be repaired, so the method table adds these corresponding flags;
    • Code Property:
      • The code in the method body is placed inside the "code" attribute;
    • Method table Collection Principle
      • The method does not overwrite (override), and the information of the parent class is not written to the method table of the child class;
      • It is possible for the compiler to automatically add some methods, typically such as the class constructor < Clinit >, method & the < init > method of the instance constructor;
      • Overload (Overload) a method that needs to add a feature signature, which is a collection of field symbol references in a method for each parameter in a constant pool;

8. Attribute table collection (Attribute_info)
These tables need to carry some of their own properties to describe their own special environmental information, such as Innderclasses, Linenumbertable, Code, and so on;

    • Code (term description codes)
      • Max_stack: The maximum value of the stack depth at which the JVM runs, allocating the stack frame's stack depth according to this value;
      • Max_locals: Represents the storage space required for a local variable table.
        • Slot: The smallest unit of memory allocated by a virtual machine to a local variable
          • Byte, char, float, int, short, Boolean, returnaddress length less than 32 bits, accounting for 1 slots
          • Double, long 64 bits, 2 slots
        • When the code is outside the scope of a local variable, the slot used by the local variable can be consumed by other local variables
      • Code_length: Byte code length
      • Code: Storing bytecode directives
      • 65535 limitations: Virtual machines stipulate that a method does not allow more than 65,535 bytecode, otherwise the compilation does not pass;
      • Execution: Data exchange during execution, method invocation and other operations are based on the stack (Operation Stack);
      • This keyword: In an instance method, you can usually have the this keyword to refer to a variable of the current object, because this is automatically added to the local variable table in the Java compilation.
    • Linenumbertable: Describes the Java source code line number and byte code line number;
    • Localvariabletable: Describes the relationship between variables in a local variable table and variables defined in Java source code;

Three. Byte code instruction
1, byte code composition

    • OpCode (Opcode): I (mnemonic) represents the int type data operation .... And so on
    • Operand (operands): Always an object of an array type;

The Java Virtual machine uses a schema that targets the operand stack instead of the register, and the bytecode instruction set is an instruction set schema. The operand alignment is discarded, and the fill symbol and interval are omitted.
2. Loading and storing instructions
Transfers data back and forth between the local variable table and the operand stack in the frame stack.

    • Loading a local variable into the Operation stack;
    • Stores a numeric value from the operand stack to a local variable table;
    • Loads a constant into the operand stack;
    • An instruction that expands the access index of a local variable table;

3. Operation Instruction

    • The values on the two operand stacks are given a certain operation and the results are re-deposited to the top of the Operation Stack;
    • Java does not directly support byte, short, Char, and Boolean types, all of which are converted to int types and replaced with an int directive;

4. Type conversion Instruction

    • Wide Conversion
      • int to long, float, double
      • Long to float, double
      • float to Double
    • Narrowing conversion
      • Declaration conversions that must be displayed
      • There is overflow or loss of sperm, but will not throw an exception

5. Synchronous instruction

    • The Java Virtual machine supports method-level synchronization and method internal sequence synchronization, both of which are supported by the "pipe process";
    • The execution thread is required to successfully hold the "pipe" before executing the method, and the final method is completed before releasing the "pipe".
    • The Java virtual machine is paired with Monitorenter and monitorexit two instructions, and the compiler automatically adds an exception handler. When an exception occurs, the exception handler is able to catch all exceptions and release the "pipe", monitorexit instruction response. In this way, Monitorenter and monitorexit are guaranteed to appear in pairs.

Three. Code examples

1. java files:

1 Package COM.XXX.CCC;2  PublicFinalclassInitconfig {3      Public StaticFinal Initconfig Bfcaccount =NewInitconfig (0,"AAA","AAA");4     Private intMindex;5     PrivateString Mdata;6     PrivateString Mdescribe;7     PrivateInitconfig (intIndexflag, string data, string describe) {8          This. Mindex =Indexflag;9          This. Mdata =data;Ten          This. Mdescribe =describe; One     } A      PublicString Getmdata () { -         return  This. Mdata; -}

2. Class File:

1Last modified .-7-4; Size1050bytes2 MD5 Checksum 2beb0c10f91b793c3570edcf2d1eff783Compiled from "Initconfig.java"4  PublicFinalclassCom.xxx.xxx.InitConfig5Minor version:0  //Minor version number6Major version:Wuyi //Major Version number7Flags:acc_public, Acc_final, Acc_super//Access Flags8Constant Pool://Constant Pool9#1= MethodRef # -.# A        //java/lang/object. " <init> ":() VTen#2= Fieldref #5.# the         //com/xxx/xxx/initconfig.mindex:i One#6= Class # $            //Com/xxx/xxx/common/constant/constdata A#7= String # -            //AAA -# at= Utf8 <init> -# -= Utf8 (iljava/lang/string; ljava/lang/String;) V the# -=Utf8 Code -# -= Utf8 linenumbertable//Java source line number and byte code line number -# -= Utf8 localvariabletable//The relationship between variables in a local variable table and variables defined in Java source code -# -= Utf8 This +# +=Utf8 Getmdata -# -= Utf8 () ljava/lang/String; +#Panax Notoginseng= Utf8 <clinit> A# -=Utf8 () V at# +=Utf8 Initconfig.java -# A= Nameandtype # at:# -        //"<init>":() V -# $= Utf8 com/xxx/xxx/initconfig -# $= Utf8 com/xxx/xxx/common/constant/Constdata -# -= Nameandtype # -:# -        //Seaaccount:lcom/xxx/ccc/initconfig; -# Wu= Utf8 java/lang/Object in  Public Staticfinal com.xxx.xxx Bfcaccount; -descriptor:lcom/xxx/xxx/initconfig; to flags:acc_public, acc_static, acc_final +  Publicjava.lang.String getmdata (); -Descriptor: () ljava/lang/String; the Flags:acc_public * Code: $stack=1, locals=1, args_size=1Panax Notoginseng             0: Aload_0 -             1: GetField #3                  //Field mdata:ljava/lang/string; the          4: Areturn +Linenumbertable://Java source line number and byte code line number ALine $:0 theLocalvariabletable://The relationship between variables in a local variable table and variables defined in Java source code + Start Length Slot Name Signature -                0       5     0    ThisLcom/xxx/xxx/initconfig;//method adds a default this

Four. Summary

Why some "non-Java" language can also run on the JVM, this is because the JVM only know the class file, so if the final compiler of a language is a class file, then there is no difference for the JVM, but according to the structure of the class file, it will not be Yes. The Class defines a number of specific basic types and table structures, which allow the JVM to recognize the file through the magic number, which is guaranteed to run on the required JDK version, a constant in the constant pool, and access flag bits to determine access rights. Indexed collections make it easy to keep in touch with the outside class, the field table holds the variables we define, the method table stores the information, and the attribute table stores some of the properties of the various tables described above. Remember that slots are the smallest unit of memory allocated for local variables, and slots can be used by other replacements when the program goes out of scope. Here, the code is just the most static storage format, the program to run up. The operation instructions are also required and are stored by bytecode, including opcode and operands. There are load storage, operations, type conversions, synchronization instructions.

Deep understanding of Java Virtual Machine 04--class structure files

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.