Java Virtual Machine knowledge -class 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.

Java Virtual Machine knowledge -class file

ClassFile structure description
magic: Magic Number. The only role is to determine whether the file is a file class virtual machine can be received. Fixed values: 0xCAFEBABE.
minor_version: Minor version class file.
major_version: major version number of class files.
constant_pool_count: constant pool counter.
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.
access_flags: access flag. It is used to represent and access the properties of a class or interface.
this_class: The current class index.
super_class: parent index.
interfaces_count: interface counters. The number of direct superclass of the current class or interface.
interfaces [interfaces_count]: Interface Table.
fields_count: field counter. The number of members of the class files in the current field.
fields [fields_count]: field tables.
methods_count: counter method. The number of methods.
methods [methods_count]: a method table.
attributes_count: property counters. The number of attributes.
attributes [attributes_count]: property sheet.
An example
of the following classes compiled Test2.class.

class Test2 {public
public static int. 1 = I;
public static void main () {
System.out.println (I);
}
}
open Test2.class by hexadecimal viewer.

Java Virtual Machine knowledge -class file

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

Test2 public ();
descriptor: () V
the flags: ACC_PUBLIC
Code:
Stack = 2, about locals =. 1, args_size. 1 =
0: aload_0
. 1: Method, the invokespecial #. 1 // Java / lang / Object "<the init>" :(. ) V
. 4: aload_0
. 5: iconst_1
. 6: Field, PutField # 2 // I: the I
. 9: return
LineNumberTable:
Line. 1: 0
Line 2:. 4
}
the SourceFile: "Test2.java"
the Java virtual machine limits
of each class or interface constant pool of up to 65,535.
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.
Like the author of this article can give a point endorsed, look, will share Java-related articles every day! There are benefits presented from time to time, including the consolidation of learning materials, interview questions, such as source code ~ ~

Guess you like

Origin blog.51cto.com/14440597/2424364