一个java程序的执行按照以下的步骤进行
JVM运行时数据区中各部分的作用:
编写一个示例源代码Procedure.java。
Procedure.java
package test;
public class Procedure {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(a + b);
}
}
使用javac Procedure.java
将源代码Procedure.java编译成Procedure.class字节码文件。
sublime可直接查看.class文件内容
cafe babe 0000 0034 001b 0a00 0500 0e09
000f 0010 0a00 1100 1207 0013 0700 1401
0006 3c69 6e69 743e 0100 0328 2956 0100
0443 6f64 6501 000f 4c69 6e65 4e75 6d62
6572 5461 626c 6501 0004 6d61 696e 0100
1628 5b4c 6a61 7661 2f6c 616e 672f 5374
7269 6e67 3b29 5601 000a 536f 7572 6365
4669 6c65 0100 0e50 726f 6365 6475 7265
2e6a 6176 610c 0006 0007 0700 150c 0016
0017 0700 180c 0019 001a 0100 0e74 6573
742f 5072 6f63 6564 7572 6501 0010 6a61
7661 2f6c 616e 672f 4f62 6a65 6374 0100
106a 6176 612f 6c61 6e67 2f53 7973 7465
6d01 0003 6f75 7401 0015 4c6a 6176 612f
696f 2f50 7269 6e74 5374 7265 616d 3b01
0013 6a61 7661 2f69 6f2f 5072 696e 7453
7472 6561 6d01 0007 7072 696e 746c 6e01
0004 2849 2956 0021 0004 0005 0000 0000
0002 0001 0006 0007 0001 0008 0000 001d
0001 0001 0000 0005 2ab7 0001 b100 0000
0100 0900 0000 0600 0100 0000 0700 0900
0a00 0b00 0100 0800 0000 3200 0300 0300
0000 0e04 3c05 3db2 0002 1b1c 60b6 0003
b100 0000 0100 0900 0000 1200 0400 0000
0900 0200 0a00 0400 0b00 0d00 0c00 0100
0c00 0000 0200 0d
然后使用javap -v Procedure.class>
查看字节码文件解析内容。
Procedure.class
Classfile /E:/workspace/IdeaProject/java/src/test/Procedure.class
Last modified 2021-12-29; size 407 bytes
MD5 checksum 1ac66dc40972a468ce317bf3232655a7
Compiled from "Procedure.java"
public class test.Procedure
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #5.#14 // java/lang/Object."<init>":()V
#2 = Fieldref #15.#16 // java/lang/System.out:Ljava/io/PrintStream;
#3 = Methodref #17.#18 // java/io/PrintStream.println:(I)V
#4 = Class #19 // test/Procedure
#5 = Class #20 // java/lang/Object
#6 = Utf8 <init>
#7 = Utf8 ()V
#8 = Utf8 Code
#9 = Utf8 LineNumberTable
#10 = Utf8 main
#11 = Utf8 ([Ljava/lang/String;)V
#12 = Utf8 SourceFile
#13 = Utf8 Procedure.java
#14 = NameAndType #6:#7 // "<init>":()V
#15 = Class #21 // java/lang/System
#16 = NameAndType #22:#23 // out:Ljava/io/PrintStream;
#17 = Class #24 // java/io/PrintStream
#18 = NameAndType #25:#26 // println:(I)V
#19 = Utf8 test/Procedure
#20 = Utf8 java/lang/Object
#21 = Utf8 java/lang/System
#22 = Utf8 out
#23 = Utf8 Ljava/io/PrintStream;
#24 = Utf8 java/io/PrintStream
#25 = Utf8 println
#26 = Utf8 (I)V
{
public test.Procedure();
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 7: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=3, args_size=1
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
7: iload_1
8: iload_2
9: iadd
10: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
13: return
LineNumberTable:
line 9: 0
line 10: 2
line 11: 4
line 12: 13
}
SourceFile: "Procedure.java"
字节码文件解析内容分析
1、版本号/访问控制
版本号对应关系
版本号规则: JDK5,6,7,8分别对应49,50,51,52
访问标志
2、常量池
类型解析
3、构造方法
4、程序入口main方法
JVM指令码参考指令码表:https://blog.csdn.net/weixin_43598687/article/details/122219111
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43598687/article/details/122203848
内容来源于网络,如有侵权,请联系作者删除!