java class class file structure

Taking advantage of the May Day holiday, I took a look at the class file structure of java, and recorded my study notes here.

 

bytecode

The storage format of the program. Language is irrelevant.

Cross-platform.

class file - overview

  • Contains the Java virtual machine instruction set and symbol table , as well as several other auxiliary information.
  • A class file is a set of binary streams in 8-bit byte-based units. Big-endian storage.

class file - structure

  1. Magic number, version of the class file
    Magic number: Make sure this file can be accepted by the virtual machine. 4 bytes: 0xCAFEBABE
    Version: Immediately after the magic number. 4 bytes.
              The 5th and 6th bytes identify the minor version number.
              The 7th and 8th bytes identify the major version number.
     
  2. The constant pool is
    followed by the version number, the resource repository.
    It mainly stores two types of constants: literals (text strings, constant values ​​declared as final, etc.), and symbolic references.
    The java code javac is compiled and dynamically linked when the virtual machine loads the class file.
    When the virtual machine is running, it needs to obtain the corresponding symbol reference from the constant pool, and then parse and translate it into the specific memory address when the class is created or run.
    In Java, if you define a variable or method name with more than 64KB English characters, it will not be parsed.
     
  3. Access flags
    are used to identify some class or interface level access information.
    Such as public, final, etc.
     
  4. Class index, parent class index, interface index collection
    Use these three data to determine the inheritance relationship of this class.
    Class Index: Determines the fully qualified name of this class.
    Parent class index: Used to determine the fully qualified name of the class's parent class.
    Interface index collection: used to describe which interfaces this class implements.
     
  5. The field table collection is
    used to describe the variables declared in the interface or class.
    Included information: field's scope, instance variable or class variable, mutability, concurrency visibility, serializable, field data type, field name.
     
  6. Method table collection
    The description of the method, similar to the field table collection.
    The java code in the method, stored in the "Code" property.
    In Java, to overload a method, it is required to have a different signature than the original method.
     
  7. Attribute table set
    In the class file, field table, and method table, you can carry your own attribute table set to describe the specific information of some scenarios.

Introduction to Bytecode Instructions

It consists of a byte-length number representing the meaning of a specific operation, followed by 0 or more parameters required for this operation.

  1. Bytecode
    Most of the instructions do not support byte, char, short, or even no boolean type at all. The compiler will sign-extend data of type byte and short to corresponding data of type int at compile time or run time, and zero-bit-extend data of type boolean and char to data of corresponding int type.

    Most operations on boolean, byte, short, and char type data actually use the corresponding int type as the operation type.
     
  2. Load and store instructions
    are used to transfer data to and from the local variable table in the stack frame and the operand stack.
     
  3. Operation instructions
    are used to perform a specific operation on the values ​​on the two operand stacks and restore the result to the top of the operation stack.
    There are two types: instructions that operate on integer data and instructions that operate on floating-point data.
    Rounding operation: round-to-nearest mode.
     
  4. Type conversion instructions
    Convert two different numeric types to each other.
    Widening type conversion, narrowing type conversion.
     
  5. Object creation and access instructions
    new, newarray, getfield, putfield, baload, bastore, arraylength, instanceof, etc.
     
  6. Operand stack management instructions
    pop, dup, swap, etc.
     
  7. Control transfer instructions
    conditionally or unconditionally modify the value of the PC register.
     
  8. Method call and return instructions
    invokevirtual, invokeinterface, invokespecial, invokestatic, invokedynamic, etc.
     
  9. Exception handling instruction
    Display throwing exception: athrow;
    the virtual machine detects that the exception field is thrown: such as ArithmeticException.
     
  10. Synchronization instructions
    are implemented using monitors.

 

After reading it initially, some places are still a bit difficult to understand, and I feel that I will forget it after a while. Only in the future will continue to think and review to deepen the impression.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325144119&siteId=291194637