字节码在JVM中的执行过程分析

通过简单的代码(暂时不涉及到堆内存 - -),查看相应的指令以及在JVM内存中执行的过程。

public static void main(String[] args) {
    	int a = 100;
    	int b = 200;
    	int c = a / b;
    	System.out.println(c);
    }

通过指令javap -c -v SimplePrint.class
得到字节码文件

Classfile /F:/demos/target/classes/com/apple/SimplePrint.class
  Last modified 2020-7-14; size 590 bytes
  MD5 checksum b4fc934e0f74041e002e77db8aa0f86d
  Compiled from "SimplePrint.java"
public class com.apple.SimplePrint
  SourceFile: "SimplePrint.java"
  minor version: 0
  major version: 52
  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/apple/SimplePrint
   #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/apple/SimplePrint;
  #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               SimplePrint.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/apple/SimplePrint
  #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.apple.SimplePrint();
    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 7: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0       5     0  this   Lcom/apple/SimplePrint;

  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC

    Code:
      stack=2, locals=4, args_size=1
         0: bipush        100
         2: istore_1      
         3: sipush        200
         6: istore_2      
         7: iload_1       
         8: iload_2       
         9: idiv          
        10: istore_3      
        11: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
        14: iload_3       
        15: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
        18: return        
      LineNumberTable:
        line 9: 0
        line 10: 3
        line 11: 7
        line 12: 11
        line 13: 18
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0      19     0  args   [Ljava/lang/String;
               3      16     1     a   I
               7      12     2     b   I
              11       8     3     c   I
}

把main方法具体的字节码指令截取出来,我们一步步的分析。

在这里插入图片描述
main方法的描述信息,public,static 以及参数等。

在这里插入图片描述
stack=2 栈的深度为2
locasl=4 局部变量表为4
args_size=1 参数个数为1

局部变量表如下:
局部变量表
在这里插入图片描述
ps. 画的有点丑有点随意- -
栈是遵循先进后出的原则,我这里画的比较随意,主要是简单的介绍进栈和出栈的流程。
局部变量表一开始就确定了大小是4,我这里没画全,大家知道一下~

到此,栈里是空的,而局部变量表里有三个数据,分别为locals[0] = args, locals[1] = 100,locals[2] = 200

在这里插入图片描述

此时pc到7,我们继续往下走。

在这里插入图片描述

到此,栈里是空的,而局部变量表里有三个数据,分别为locals[0] = args, locals[1] = 100,locals[2] = 200,locals[3] = 2
在这里插入图片描述

此时pc到11,我们继续往下走。

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值