欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jvm字节码指令

程序员文章站 2022-04-18 15:52:58
...

1.示例

package com.yalong.jvmclass;

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

直接反编辑Test.class文件:javap -verbose Test.class,反编译出来的内容有类信息、符号引用、字节码信息,更能清晰地了解java虚拟机如何执行java代码

Classfile /E:/soft/idea/work_place/Java-Foundation/target/classes/com/yalong/jvmclass/Test.class
  Last modified 2019-8-14; size 586 bytes
  MD5 checksum 63400a58bf99c68b24c93e0187bc7203
  Compiled from "Test.java"
public class com.yalong.jvmclass.Test
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool: # 常量池
   #1 = Methodref          #5.#23         // java/lang/Object."<init>":()V
   #2 = Fieldref           #24.#25        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Methodref          #26.#27        // java/io/PrintStream.println:(I)V
   #4 = Class              #28            // com/yalong/jvmclass/Test
   #5 = Class              #29            // java/lang/Object
   #6 = Utf8               <init>
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LineNumberTable
  #10 = Utf8               LocalVariableTable
  #11 = Utf8               this
  #12 = Utf8               Lcom/yalong/jvmclass/Test;
  #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               SourceFile
  #22 = Utf8               Test.java
  #23 = NameAndType        #6:#7          // "<init>":()V
  #24 = Class              #30            // java/lang/System
  #25 = NameAndType        #31:#32        // out:Ljava/io/PrintStream;
  #26 = Class              #33            // java/io/PrintStream
  #27 = NameAndType        #34:#35        // println:(I)V
  #28 = Utf8               com/yalong/jvmclass/Test
  #29 = Utf8               java/lang/Object
  #30 = Utf8               java/lang/System
  #31 = Utf8               out
  #32 = Utf8               Ljava/io/PrintStream;
  #33 = Utf8               java/io/PrintStream
  #34 = Utf8               println
  #35 = Utf8               (I)V
{
  public com.yalong.jvmclass.Test();
    descriptor: ()V # 构造函数
    flags: 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   Lcom/yalong/jvmclass/Test;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      # 操作数栈的深度2
      # 本地变量表最大长度(slot为单位),64位的是2,其他是1,索引从0开始,如果是非static方法索引0代表this,后面是入参,后面是本地变量
      # 1个参数,实例方法多一个this参数
      stack=2, locals=4, args_size=1
         0: iconst_1 #常量1压栈
         1: istore_1 #出栈保存到本地变量1里面
         2: iconst_2 #常量2压栈
         3: istore_2 #出栈保存到本地变量2里面
         4: iload_1 #局部变量1压栈
         5: iload_2 #局部变量2压栈
         6: iadd    # 栈顶两个元素相加,计算结果压栈
         7: istore_3 # 出栈保存到局部变量3里面
         8: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream; # 获取一个PrintStream引用
        11: iload_3
        12: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V # 执行方法引用的方法
        15: return
      LineNumberTable: # 行号
        line 7: 0 # 代码里的第7行代表的Code下的标号为0的指令,也有可能是连续几个指令
        line 8: 2
        line 9: 4
        line 10: 8
        line 11: 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
}
SourceFile: "Test.java"