JavaSE---java命令---javap
1、javap
1.1 javap是JDK自带的反解析工具;
1.2 作用:就是根据class字节码文件,反解析出 当前类 对应的code区(汇编指令)、本地变量表、异常表和代码行偏移量映射表、常量池等等信息;
1.3 常用的命令:
javap -c
对当前class字节码进行反编译生成汇编代码;
javap -l
会输出行号和本地变量表信息;
javap -v
会输出行号、本地变量表信息、反编译汇编代码,还会输出当前类用到的常量池等信息;
1.4 案例:
package yunsuanfu; public class Test { public static void main(String[] args){ plusTest(); plusTestTwo(); } /** * a++:先参与运算(System.out.println(a))、再进行自增(a++ 等效于a=a+1) * ++a:先自增(a++),再参与运算(System.out.println(a)) * * 结果是:9、11 */ public static void plusTest(){ int a=9; System.out.println(a++); System.out.println(++a); } /** * y+=z--/++x 等效于 y=y+z--/++x y=2+3/2 * * 结果是:3 */ public static void plusTestTwo(){ int x=1,y=2,z=3; System.out.println(y+=z--/++x); } }
javap -c Test.class
Compiled from "Test.java"
public class yunsuanfu.Test {
public yunsuanfu.Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: invokestatic #2 // Method plusTest:()V
3: invokestatic #3 // Method plusTestTwo:()V
6: return
public static void plusTest();
Code:
0: bipush 9
2: istore_0
3: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
6: iload_0
7: iinc 0, 1
10: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
13: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
16: iinc 0, 1
19: iload_0
20: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
23: return
public static void plusTestTwo();
Code:
0: iconst_1
1: istore_0
2: iconst_2
3: istore_1
4: iconst_3
5: istore_2
6: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
9: iload_1
10: iload_2
11: iinc 2, -1
14: iinc 0, 1
17: iload_0
18: idiv
19: iadd
20: dup
21: istore_1
22: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
25: return
}