|
留言簿(2)
随笔分类
文章分类
文章档案
搜索
最新评论
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
class文件格式如下:
- 相信学java的人都对new Object();创建对象都很熟悉,但想要真正了解原理就没那么容易!以以下例子为例,解释class代码及执行过程,如有错误,还望各位高手多多指教!
-
-
- public class Dog {
- public String name;
- public int age;
- public Dog() {
- }
- public Dog(String name)
- {
- this.name = name;
- }
- public Dog(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
- public static void getStaticValue(int j)
- {
- int i=j;
- System.out.println(i);
- }
- public void getValue(int j)
- {
- int i=j;
- System.out.println(i);
- }
- public static void main(String[] args) {
- try {
- new Dog().getValue(10);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- 如上代码很简单,main方法加上try catch只是为了 让大家看一下java文件生产字节码是怎么样的
-
- 执行javac Dog.java文件,生成class文件。然后使用javap -verbose Dog反编译出class代码。
-
- 生成如下代码:
-
- view plaincopy to clipboardprint?
- Compiled from "Dog.java"
- public class Dog extends java.lang.Object
- SourceFile: "Dog.java"
- minor version: 0
- major version: 50
-
-
- Constant pool:
-
-
-
-
- 如下 #11.#31; 这段代码什么作用呢?很简单,就是表示创建Object对象。
- 首先来看.号左边的#11,找到常量池中#11,跳到const #11 = class #42;
- 这个表示是一个Object类型,class后面的 #42;表示接下来要跳到常量池中#42
- 找到const #42 = Asciz java/lang/Object; 表示生成Object类型
- 接下来看.号右边的#31。
- const #31 = NameAndType #16:#17;
- const #16 = Asciz <init>;
- const #17 = Asciz ()V;
-
-
-
-
-
-
- 如下有 :
- Method
- Field
- class
- Asciz
- NameAndType
-
- java类型对应的class文件方法签名的标识符:
- invokespecial
- invokevirtual
- invokestatic
-
- Ljava/lang/String;;
- void V
- int char byte short long float double 都是类型第一个字母(大写)
- boolean 比较特别,用Z表示 ,因为B被byte给占用了
-
-
- const #1 = Method #11.#31;
-
- const #2 = Field #6.#32;
- const #3 = Field #6.#33;
- const #4 = Field #34.#35;
- const #5 = Method #36.#37;
- const #6 = class #38;
- const #7 = Method #6.#31;
- const #8 = Method #6.#39;
- const #9 = class #40;
- const #10 = Method #9.#41;
- const #11 = class #42;
- const #12 = Asciz name;
- const #13 = Asciz Ljava/lang/String;;
- const #14 = Asciz age;
- const #15 = Asciz I;
- const #16 = Asciz <init>;
- const #17 = Asciz ()V;
- const #18 = Asciz Code;
- const #19 = Asciz LineNumberTable;
- const #20 = Asciz (Ljava/lang/String;)V;
- const #21 = Asciz (Ljava/lang/String;I)V;
- const #22 = Asciz getStaticValue;
- const #23 = Asciz (I)V;
- const #24 = Asciz getValue;
- const #25 = Asciz main;
- const #26 = Asciz ([Ljava/lang/String;)V;
- const #27 = Asciz StackMapTable;
- const #28 = class #40;
- const #29 = Asciz SourceFile;
- const #30 = Asciz Dog.java;
- const #31 = NameAndType #16:#17;
- const #32 = NameAndType #12:#13;
- const #33 = NameAndType #14:#15;
- const #34 = class #43;
- const #35 = NameAndType #44:#45;
- const #36 = class #46;
- const #37 = NameAndType #47:#23;
- const #38 = Asciz Dog;
- const #39 = NameAndType #24:#23;
- const #40 = Asciz java/lang/Exception;
- const #41 = NameAndType #48:#17;
- const #42 = Asciz java/lang/Object;
- const #43 = Asciz java/lang/System;
- const #44 = Asciz out;
- const #45 = Asciz Ljava/io/PrintStream;;
- const #46 = Asciz java/io/PrintStream;
- const #47 = Asciz println;
- const #48 = Asciz printStackTrace;
-
- {
- public java.lang.String name;
-
- public int age;
-
-
-
-
-
-
-
-
-
-
- public Dog();
- Code:
- Stack=1, Locals=1, Args_size=1
- 0: aload_0
- 1: invokespecial #1;
- 4: return
- LineNumberTable:
- line 6: 0
- line 7: 4
-
-
-
-
- public Dog(java.lang.String);
- Code:
- Stack=2, Locals=2, Args_size=2
- 0: aload_0
- 1: invokespecial #1;
-
- 4: aload_0
- 5: aload_1
- 6: putfield #2;
- 9: return
- LineNumberTable:
- line 10: 0
- line 11: 4
- line 12: 9
-
-
- public Dog(java.lang.String, int);
- Code:
- Stack=2, Locals=3, Args_size=3
- 0: aload_0
- 1: invokespecial #1;
- 4: aload_0
- 5: aload_1
- 6: putfield #2;
- 9: aload_0
- 10: iload_2
- 11: putfield #3;
- 14: return
- LineNumberTable:
- line 15: 0
- line 16: 4
- line 17: 9
- line 18: 14
-
-
- public static void getStaticValue(int);
- Code:
- Stack=2, Locals=2, Args_size=1
- 0: iload_0
- 1: istore_1
- 2: getstatic #4;
- 5: iload_1
- 6: invokevirtual #5;
- 9: return
- LineNumberTable:
- line 21: 0
- line 22: 2
- line 23: 9
-
-
- public void getValue(int);
- Code:
- Stack=2, Locals=3, Args_size=2
- 0: iload_1
- 1: istore_2
- 2: getstatic #4;
- 5: iload_2
- 6: invokevirtual #5;
- 9: return
- LineNumberTable:
- line 26: 0
- line 27: 2
- line 28: 9
-
-
- public static void main(java.lang.String[]);
- Code:
- Stack=2, Locals=2, Args_size=1
- 0: new #6;
- 3: dup
- 4: invokespecial #7;
- 7: bipush 10
- 9: invokevirtual #8;
- 12: goto 20
- 15: astore_1
- 16: aload_1
- 17: invokevirtual #10;
- 20: return
- Exception table:
- from to target type
- 0 12 15 Class java/lang/Exception
-
-
-
- LineNumberTable:
- line 32: 0
- line 35: 12
- line 33: 15
- line 34: 16
- line 36: 20
-
-
-
-
-
- StackMapTable: number_of_entries = 2
- frame_type = 79
- stack = [ class java/lang/Exception ]
- frame_type = 4
-
-
- }
|
|