读《深入理解Java虚拟机》有感——第一部分:Class文件的结构
1.产生
源码(.java文件)——>编译器(如:javac)——>字节码(.class文件)——>虚拟机(如:HotSpot)执行
2.Class文件
1)构成:
2)例子:
[1] .java文件
桌面/test/test/Father.java、桌面/test/test/Father_interface.java、桌面/test/Son.java ;
[2]编译出.class文件
cd 桌面/test;
桌面/test>javac Son.java;
[3]查看.class文件
桌面/test>javap -verbose Son;
Classfile /C:/Users/Administrator/Desktop/test/Son.class
Last modified 2016-4-11; size 1005 bytes
MD5 checksum 7d138d76267a6bb372eba05296e2c932魔数
Compiled from "Son.java"
public class test.Son extends test.Father implements test.Father_interface索引(1类索引、2父类索引、3接口索引,指向常量池)
SourceFile: "Son.java"
minor version: 0
major version: 51主板本号
flags: ACC_PUBLIC, ACC_SUPER访问标志<当前类>
Constant pool:常量池
#1 = Methodref #16.#33 // test/Father."<init>":()V 符号引用{
#2 = Fieldref #15.#34 // test/Son.sId_literal:I
#3 = Fieldref #35.#36 // java/lang/System.out:Ljava/io/PrintStream;
#4 = Class #37 // java/lang/StringBuilder
#5 = Methodref #4.#33 // java/lang/StringBuilder."<init>":()V
#6 = String #38 // statical_sId =
#7 = Methodref #4.#39 // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/Strin
gBuilder;
#8 = Fieldref #15.#40 // test/Son.statical_sId:I
#9 = Methodref #4.#41 // java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
#10 = Methodref #4.#42 // java/lang/StringBuilder.toString:()Ljava/lang/String;
#11 = Methodref #43.#44 // java/io/PrintStream.println:(Ljava/lang/String;)V
#12 = String #45 // Son实例构造器!
#13 = String #46 // hello!this is son!
#14 = String #47 // Son类构造器<clinit>执行!
#15 = Class #48 // test/Son 1
#16 = Class #49 // test/Father 2
#17 = Class #50 // test/Father_interface 3 }符号引用
#18 = Utf8 final_sId
#19 = Utf8 I
#20 = Utf8 ConstantValue
#21 = Integer 123 字面量
#22 = Utf8 statical_sId
#23 = Utf8 sId_literal
#24 = Utf8 sId_nonLiteral
#25 = Utf8 <init>
#26 = Utf8 ()V
#27 = Utf8 Code
#28 = Utf8 LineNumberTable
#29 = Utf8 sayHello
#30 = Utf8 <clinit>
#31 = Utf8 SourceFile
#32 = Utf8 Son.java 符号引用{
#33 = NameAndType #25:#26 // "<init>":()V
#34 = NameAndType #23:#19 // sId_literal:I
#35 = Class #51 // java/lang/System
#36 = NameAndType #52:#53 // out:Ljava/io/PrintStream;
#37 = Utf8 java/lang/StringBuilder
#38 = Utf8 statical_sId =
#39 = NameAndType #54:#55 // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
#40 = NameAndType #22:#19 // statical_sId:I
#41 = NameAndType #54:#56 // append:(I)Ljava/lang/StringBuilder;
#42 = NameAndType #57:#58 // toString:()Ljava/lang/String;
#43 = Class #59 // java/io/PrintStream
#44 = NameAndType #60:#61 // println:(Ljava/lang/String;)V
#45 = Utf8 Son实例构造器! }符号引用
#46 = Utf8 hello!this is son!
#47 = Utf8 Son类构造器<clinit>执行!
#48 = Utf8 test/Son
#49 = Utf8 test/Father
#50 = Utf8 test/Father_interface
#51 = Utf8 java/lang/System
#52 = Utf8 out
#53 = Utf8 Ljava/io/PrintStream;
#54 = Utf8 append
#55 = Utf8 (Ljava/lang/String;)Ljava/lang/StringBuilder;
#56 = Utf8 (I)Ljava/lang/StringBuilder;
#57 = Utf8 toString
#58 = Utf8 ()Ljava/lang/String;
#59 = Utf8 java/io/PrintStream
#60 = Utf8 println
#61 = Utf8 (Ljava/lang/String;)V
{
public test.Son();方法表——实例构造器
flags: ACC_PUBLIC
Code:
stack=3, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method test/Father."<init>":()V
4: aload_0
5: bipush 123
7: putfield #2 // Field sId_literal:I
10: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
13: new #4 // class java/lang/StringBuilder
16: dup
17: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
20: ldc #6 // String statical_sId =
22: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/
lang/StringBuilder;
25: getstatic #8 // Field statical_sId:I
28: invokevirtual #9 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilde
r;
31: invokevirtual #10 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
34: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
37: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
40: ldc #12 // String Son实例构造器!
42: invokevirtual #11 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
45: sipush 333
48: putstatic #8 // Field statical_sId:I
51: return
LineNumberTable:
line 15: 0
line 7: 4
line 16: 10
line 17: 37
line 18: 45
line 19: 51