Java Virtual Machine class knowledge [file]

  Class file is a set of 8-bit binary stream in units of bytes based on respective data items are arranged in the exact order compact Class file not added any intermediate separators, which makes the contents of the entire file is stored in Class almost all procedures data necessary to run, there is no gap. When faced with the need to occupy more than 8 bytes of data items, in accordance with the embodiment will be divided into several endian byte 8 are stored.

ClassFile structure

ClassFile Structure Description

  1. magic: the magic number. The only role is to determine whether the file is a file class virtual machine can be received. Fixed values: 0xCAFEBABE.

  2. minor_version: Minor version class file.

  3. major_version: major version number of class files.

  4. constant_pool_count: constant pool counter.

  5. constant_pool [constant_pool_count]: constant pool. Contains the string constants, class or interface name, field names and other constants. The constant pool each of which have the same characteristics, i.e., a byte as the first type of marker is used to determine the format.

  6. access_flags: access flag. It is used to represent and access the properties of a class or interface.

  7. this_class: The current class index.

  8. super_class: parent index.

  9. interfaces_count: interface counters. The number of direct superclass of the current class or interface.

  10. interfaces [interfaces_count]: Interface Table.

  11. fields_count: field counter. The number of members of the class files in the current field.

  12. fields [fields_count]: field tables.

  13. methods_count: counter method. The number of methods.

  14. methods [methods_count]: a method table.

  15. attributes_count: property counters. The number of attributes.

  16. attributes [attributes_count]: property sheet.

one example

The following classes of compiled Test2.class.

public class Test2 {
    public static int i = 1;
    public static void main() {
        System.out.println(i);
    }
}

Open by hexadecimal viewer Test2.class.

Hex

The ClassFile structure description, the first four-byte magic number oxcafebabe. Next is the minor version number and major version number ox0000 and ox0034, 52, 52 and 0 decimal represent JDK1.8, so the JDK version 1.8.0.

You can also use the built-in java decompiler tool to parse the bytecode files. Command javap -v Test2.class.

Classfile /E:/CODE/JVM/Test2.class
  Last modified 2019-7-19; size 219 bytes
  MD5 checksum 841c66674d71005bc6a97fe6c3b0fb1d
  Compiled from "Test2.java"
public class Test2
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#13         // java/lang/Object."<init>":()V
   #2 = Fieldref           #3.#14         // Test2.i:I
   #3 = Class              #15            // Test2
   #4 = Class              #16            // java/lang/Object
   #5 = Utf8               i
   #6 = Utf8               I
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               SourceFile
  #12 = Utf8               Test2.java
  #13 = NameAndType        #7:#8          // "<init>":()V
  #14 = NameAndType        #5:#6          // i:I
  #15 = Utf8               Test2
  #16 = Utf8               java/lang/Object
{
  public int i;
    descriptor: I
    flags: ACC_PUBLIC

  public Test2();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: iconst_1
         6: putfield      #2                  // Field i:I
         9: return
      LineNumberTable:
        line 1: 0
        line 2: 4
}
SourceFile: "Test2.java"

Java virtual machine restrictions

  • Each constant pool of the class or interface 65535.
  • The number of fields in the class or interface may be declared 65535.
  • The number of classes or methods declared in the interface can be up to 65,535.
  • The direct parent of the class or interface interfaces 65535.
  • Stack frame is created when the method is called, its local variable table of local variables of the largest digital 65535.
  • The maximum depth of the operand stack of the method frame is 65535.
  • Method parameters up to 255.
  • Name fields and methods, and methods described field identifier and the maximum length of a string value other constants 65,535 characters.
  • The maximum dimension of the array is 255-dimensional.

Reference: "in-depth understanding of the Java Virtual Machine (Second Edition)," "Java Virtual Machine Specification (Java SE version 8)."

Guess you like

Origin www.cnblogs.com/bigshark/p/11229409.html