Skip to content

JVM字节码

示例

java
package cn.diyai.mul_thread.jvmbytecode;

public class Example1 {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = a + b;
        System.out.println(c);
    }
}

javap获取字节码

javap -verbose Example1.class

输出

shell
Classfile /D:/wxmgcs/multi_thread/target/classes/cn/diyai/mul_thread/jvmbytecode/Example1.class
  Last modified 2024年2月13日; size 652 bytes
  SHA-256 checksum f9dbd6a5f81889981512a4353874604b6bdadc767a4ca416a4dd0ddea401db00
  Compiled from "Example1.java"
public class cn.diyai.mul_thread.jvmbytecode.Example1
  minor version: 0
  major version: 52
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #4                          // cn/diyai/mul_thread/jvmbytecode/Example1
  super_class: #5                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
   #1 = Methodref          #5.#24         // java/lang/Object."<init>":()V
   #2 = Fieldref           #25.#26        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Methodref          #27.#28        // java/io/PrintStream.println:(I)V
   #4 = Class              #29            // cn/diyai/mul_thread/jvmbytecode/Example1
   #5 = Class              #30            // java/lang/Object
   #6 = Utf8               <init>
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LineNumberTable
  #10 = Utf8               LocalVariableTable
  #11 = Utf8               this
  #12 = Utf8               Lcn/diyai/mul_thread/jvmbytecode/Example1;
  #13 = Utf8               main
  #14 = Utf8               ([Ljava/lang/String;)V
  #15 = Utf8               args
  #16 = Utf8               [Ljava/lang/String;
  #17 = Utf8               a
  #18 = Utf8               I
  #19 = Utf8               b
  #20 = Utf8               c
  #21 = Utf8               MethodParameters
  #22 = Utf8               SourceFile
  #23 = Utf8               Example1.java
  #24 = NameAndType        #6:#7          // "<init>":()V
  #25 = Class              #31            // java/lang/System
  #26 = NameAndType        #32:#33        // out:Ljava/io/PrintStream;
  #27 = Class              #34            // java/io/PrintStream
  #28 = NameAndType        #35:#36        // println:(I)V
  #29 = Utf8               cn/diyai/mul_thread/jvmbytecode/Example1
  #30 = Utf8               java/lang/Object
  #31 = Utf8               java/lang/System
  #32 = Utf8               out
  #33 = Utf8               Ljava/io/PrintStream;
  #34 = Utf8               java/io/PrintStream
  #35 = Utf8               println
  #36 = Utf8               (I)V
{
  public cn.diyai.mul_thread.jvmbytecode.Example1();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcn/diyai/mul_thread/jvmbytecode/Example1;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: iconst_1
         1: istore_1
         2: iconst_2
         3: istore_2
         4: iload_1
         5: iload_2
         6: iadd
         7: istore_3
         8: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
        11: iload_3
        12: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
        15: return
      LineNumberTable:
        line 5: 0
        line 6: 2
        line 7: 4
        line 8: 8
        line 9: 15
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      16     0  args   [Ljava/lang/String;
            2      14     1     a   I
            4      12     2     b   I
            8       8     3     c   I
    MethodParameters:
      Name                           Flags
      args
}
SourceFile: "Example1.java"

解析:

  1. aload_0:将当前对象引用加载到操作数栈上。
  2. invokespecial #1:调用超类中的 <init> 方法,即调用 java/lang/Object 类的构造方法。
  3. return:从当前方法返回。
  4. iconst_1:将整数常量 1 推送到栈顶。
  5. istore_1:将栈顶整数值存储到本地变量表中索引为 1 的位置。
  6. iload_1:将本地变量表中索引为 1 的整数值加载到栈顶。
  7. iadd:将栈顶两个整数值相加,并将结果推送到栈顶。
  8. getstatic #2:获取静态字段 java/lang/System.out 的值,即 java.io.PrintStream 类的静态实例。
  9. iload_3:将本地变量表中索引为 3 的整数值加载到栈顶。
  10. invokevirtual #3:调用对象的方法 println,输出整数值到控制台。
  11. return:从当前方法返回。

参考文档

Java虚拟机规范

Java语言规范

javap

字段描述符

方法描述符

字节码指令

常量池

本地变量表1

本地变量表2

操作数栈

Code属性

LineNumberTable

constant variable

常量表达式