Development tools: IEDA, JDK1.8, WinHex
A bytecode file structure
Source
package com.jalja.java.bytecode; /** * @Auther: XL * @Date: 2020/1/4 12:58 * @Description: */ public class BytecodeTest { private int num=1; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
Analysis javap -verbose a bytecode file, the magic number information is output bytecode files, version number, constant pool, class information, class constructor, a method of class information, class variables and member variables
F:\workspace\IDEA\study\jalja-base-utils\target\test-classes>javap -verbose com.jalja.java.bytecode.BytecodeTest Classfile /F:/workspace/IDEA/study/jalja-base-utils/target/test-classes/com/jalja/java/bytecode/BytecodeTest.class Last modified 2020-1-4; size 514 bytes MD5 checksum b661c2792027e7c9169a0266523412c1 Compiled from "BytecodeTest.java" public class com.jalja.java.bytecode.BytecodeTest minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #4.#20 // java/lang/Object."<init>":()V #2 = Fieldref #3.#21 // com/jalja/java/bytecode/BytecodeTest.num:I #3 = Class #22 // com/jalja/java/bytecode/BytecodeTest #4 = Class #23 // java/lang/Object #5 = Utf8 num #6 = Utf8 I #7 = Utf8 <init> #8 = Utf8 ()V #9 = Utf8 Code #10 = Utf8 LineNumberTable #11 = Utf8 LocalVariableTable #12 = Utf8 this #13 = Utf8 Lcom/jalja/java/bytecode/BytecodeTest; #14 = Utf8 getNum #15 = Utf8 ()I #16 = Utf8 setNum #17 = Utf8 (I)V #18 = Utf8 SourceFile #19 = Utf8 BytecodeTest.java #20 = NameAndType #7:#8 // "<init>":()V #21 = NameAndType #5:#6 // num:I #22 = Utf8 com/jalja/java/bytecode/BytecodeTest #23 = Utf8 java/lang/Object { public com.jalja.java.bytecode.BytecodeTest(); 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 num:I 9: return LineNumberTable: line 8: 0 line 9: 4 LocalVariableTable: Start Length Slot Name Signature 0 10 0 this Lcom/jalja/java/bytecode/BytecodeTest; public int getNum(); descriptor: ()I flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: getfield #2 // Field num:I 4: ireturn LineNumberTable: line 12: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/jalja/java/bytecode/BytecodeTest; public void setNum(int); descriptor: (I)V flags: ACC_PUBLIC Code: stack=2, locals=2, args_size=2 0: aload_0 1: iload_1 2: putfield #2 // Field num:I 5: return LineNumberTable: line 16: 0 line 17: 5 LocalVariableTable: Start Length Slot Name Signature 0 6 0 this Lcom/jalja/java/bytecode/BytecodeTest; 0 6 1 num I } SourceFile: "BytecodeTest.java"
WinHex: Open class file
Second, the magic number
1, the magic number: the first four bytes of all the class bytecode files are magic number, the magic number is a fixed value 0XCAFEBABE, JVM verifies that the data is compliant class loading.
Second, the version number
2, the version number:
After four byte magic number, version information, the first two bytes (00 00) is the minor version number (minor version: 0), the last two bytes (00 34) is the major version number (major version: 52 ), hexadecimal 34 is converted to decimal 52; 52 is the corresponding JDK1.8; therefore the version number of the class file = 1.8.0; java -version used can be verified. Since the JVM is backward compatible, so how do you bytecode file version number is less than equal to the current version of the JVM, you can run in the current JVM.
The next section: JAVA bytecode files of the constant pool