Javap命令查看反汇编字节码
-
当有人问你代码执行效率高低怎么判断时你应该怎么回答呢?
-
是靠经常分析还是有一定的理论依据呢?
-
现在可以通过Javap进行反编译,查看底层代码分析效率高低
我们编写的类文件是.java类型的文件,可以使用javac -类文件名加扩展名
命令进行编译,也可以用我们的ideal或eclipse进行打包。我们会得到.class类型的文件 -
首先定位到需要把编译(.class类型)文件的目录里
-
右击打开命令窗口输入
javap -verbose 文件名.class > test.txt
-
>号后面是输入文件为test的类型为txt的文件
-
txt文件会生成在和class文件同目录下打开
这个编译之前的代码
这是把编译生成的文件,进行了简单解释,感兴趣的小伙伴可以更深入了解
Classfile /G:test/target/test-classes/test.class
Last modified 2019-1-2; size 674 bytes
MD5 checksum abc3194b42db340c1b4a66568c418c6e
Compiled from "test.java" //类名
public class test //包名加类名,我这个没有包名所以只有类名
minor version: 0 //jdk版本号
major version: 52 //jdk版本号
flags: ACC_PUBLIC, ACC_SUPER
Constant pool: //常量池
#1 = Methodref #5.#24 // java/lang/Object."<init>":()V
#2 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
#3 = Methodref #27.#28 // java/io/PrintStream.println:(I)V
#4 = Class #29 // test
#5 = Class #30 // java/lang/Object
#6 = Utf8 <init>
#7 = Utf8 ()V
#8 = Utf8 Code
#9 = Utf8 LineNumberTable
#10 = Utf8 LocalVariableTable
#11 = Utf8 this
#12 = Utf8 Ltest;
#13 = Utf8 main
#14 = Utf8 ([Ljava/lang/String;)V
#15 = Utf8 args
#16 = Utf8 [Ljava/lang/String;
#17 = Utf8 a
#18 = Utf8 I
#19 = Utf8 b
#20 = Utf8 c
#21 = Utf8 aaa
#22 = Utf8 SourceFile
#23 = Utf8 test.java
#24 = NameAndType #6:#7 // "<init>":()V
#25 = Class #31 // java/lang/System
#26 = NameAndType #32:#33 // out:Ljava/io/PrintStream;
#27 = Class #34 // java/io/PrintStream
#28 = NameAndType #35:#36 // println:(I)V
#29 = Utf8 test
#30 = Utf8 java/lang/Object
#31 = Utf8 java/lang/System
#32 = Utf8 out
#33 = Utf8 Ljava/io/PrintStream;
#34 = Utf8 java/io/PrintStream
#35 = Utf8 println
#36 = Utf8 (I)V
{
public test();
descriptor: ()V //无参函数返回值是void
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 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Ltest;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V //返回值是void参数类型是string
flags: ACC_PUBLIC, ACC_STATIC
Code:
// stack操作数栈的深度2
// locals本地变量表最大长度(slot为单位),64位的是2,其他是1,索引从0开始,如果是非static方法索引0代表this,后面是入参,后面是本地变量
// args_size1个参数,实例方法多一个this参数
stack=2, locals=4, args_size=1
//下面是代码所对应的字节码
0: iconst_2
1: istore_1
2: iconst_3
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: istore_3
8: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
11: iload_3
12: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
15: return
LineNumberTable: //行号表,代表咱们代码行号和编译好的行号
line 9: 0
line 10: 2
line 11: 4
line 12: 8
line 13: 15
LocalVariableTable: //本地变量表
Start Length Slot Name Signature
0 16 0 args [Ljava/lang/String;
2 14 1 a I
4 12 2 b I
8 8 3 c I
}
SourceFile: "test.java"