通过简单的代码(暂时不涉及到堆内存 - -),查看相应的指令以及在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,我们继续往下走。