想必大家对这一段JAVA代码一定不会陌生:
public class Test { public static void main(String[] args) { System.out.println("Hello World!"); } }
输出:Hello World!
今天咱们就从头开始分析一下它是如何从编译到输出的。
1、编译
javac Test.java,生成JAVA的字节码文件:Test.class。使用hexdump -C Test.class查看如下:
00000000 ca fe ba be 00 00 00 32 00 1d 0a 00 06 00 0f 09 |.......2........| 00000010 00 10 00 11 08 00 12 0a 00 13 00 14 07 00 15 07 |................| 00000020 00 16 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29 |.....<init>...()| 00000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e |V...Code...LineN| 00000040 75 6d 62 65 72 54 61 62 6c 65 01 00 04 6d 61 69 |umberTable...mai| 00000050 6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 |n...([Ljava/lang| 00000060 2f 53 74 72 69 6e 67 3b 29 56 01 00 0a 53 6f 75 |/String;)V...Sou| 00000070 72 63 65 46 69 6c 65 01 00 09 54 65 73 74 2e 6a |rceFile...Test.j| 00000080 61 76 61 0c 00 07 00 08 07 00 17 0c 00 18 00 19 |ava.............| 00000090 01 00 0c 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 07 |...Hello World!.| 000000a0 00 1a 0c 00 1b 00 1c 01 00 04 54 65 73 74 01 00 |..........Test..| 000000b0 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 |.java/lang/Objec| 000000c0 74 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 |t...java/lang/Sy| 000000d0 73 74 65 6d 01 00 03 6f 75 74 01 00 15 4c 6a 61 |stem...out...Lja| 000000e0 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 |va/io/PrintStrea| 000000f0 6d 3b 01 00 13 6a 61 76 61 2f 69 6f 2f 50 72 69 |m;...java/io/Pri| 00000100 6e 74 53 74 72 65 61 6d 01 00 07 70 72 69 6e 74 |ntStream...print| 00000110 6c 6e 01 00 15 28 4c 6a 61 76 61 2f 6c 61 6e 67 |ln...(Ljava/lang| 00000120 2f 53 74 72 69 6e 67 3b 29 56 00 21 00 05 00 06 |/String;)V.!....| 00000130 00 00 00 00 00 02 00 01 00 07 00 08 00 01 00 09 |................| 00000140 00 00 00 1d 00 01 00 01 00 00 00 05 2a b7 00 01 |............*...| 00000150 b1 00 00 00 01 00 0a 00 00 00 06 00 01 00 00 00 |................| 00000160 01 00 09 00 0b 00 0c 00 01 00 09 00 00 00 25 00 |..............%.| 00000170 02 00 01 00 00 00 09 b2 00 02 12 03 b6 00 04 b1 |................| 00000180 00 00 00 01 00 0a 00 00 00 0a 00 02 00 00 00 03 |................| 00000190 00 08 00 04 00 01 00 0d 00 00 00 02 00 0e |..............| 0000019e
这是二进制文件,看不明没有关系,java提供了javap可以查看内容是什么。
2、查看CLASS文件
javap -verbose Test。其他关于javap的内容,可以查看:http://www.cnblogs.com/liqiu/p/hexdump.html
Compiled from "Test.java" public class Test extends java.lang.Object SourceFile: "Test.java" minor version: 0 major version: 50 Constant pool: const #1 = Method #6.#15; // java/lang/Object."<init>":()V const #2 = Field #16.#17; // java/lang/System.out:Ljava/io/PrintStream; const #3 = String #18; // Hello World! const #4 = Method #19.#20; // java/io/PrintStream.println:(Ljava/lang/String;)V const #5 = class #21; // Test const #6 = class #22; // java/lang/Object const #7 = Asciz <init>; const #8 = Asciz ()V; const #9 = Asciz Code; const #10 = Asciz LineNumberTable; const #11 = Asciz main; const #12 = Asciz ([Ljava/lang/String;)V; const #13 = Asciz SourceFile; const #14 = Asciz Test.java; const #15 = NameAndType #7:#8;// "<init>":()V const #16 = class #23; // java/lang/System const #17 = NameAndType #24:#25;// out:Ljava/io/PrintStream; const #18 = Asciz Hello World!; const #19 = class #26; // java/io/PrintStream const #20 = NameAndType #27:#28;// println:(Ljava/lang/String;)V const #21 = Asciz Test; const #22 = Asciz java/lang/Object; const #23 = Asciz java/lang/System; const #24 = Asciz out; const #25 = Asciz Ljava/io/PrintStream;; const #26 = Asciz java/io/PrintStream; const #27 = Asciz println; const #28 = Asciz (Ljava/lang/String;)V; { public Test(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 public static void main(java.lang.String[]); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return LineNumberTable: line 3: 0 line 4: 8 }
别看写的挺多的,分析一下么有什么:
- 首先说明一下版本和文件名之类的,至于class文件的格式,可以参考:http://blog.csdn.net/tyrone1979/article/details/964560
- 然后const的部分是将内容放入内存
- public Test()是默认的构造函数
- 有用的就是:public static void main() .............................
如果执行java Test,那么
3、申请内存栈
这部分内容比较复杂,可以参考:http://15838341661-139-com.iteye.com/blog/1287866
4、输出内容
这方面咱们比较关心,查看代码:
3: ldc #3; //String Hello World!
这句话的意思是:将字符串Hello World!放入栈顶
invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
这句话的意思是输出内容:Hello World!