文章标题 javap理解

来源:互联网 发布:360如何优化系统 编辑:程序博客网 时间:2024/06/18 08:30

javap定义
javap是 Java class文件分解器,可以反编译(即对javac编译的文件进行反编译),也可以查看java编译器生成的字节码。用于分解class文件。

测试类
// 判断一个数是否大于另一个数
class FunctionDemo{
public static void main(String[] args){
int a=10;
int b=20;
boolean is=isGreaterThan(a,b);
System.out.println(is);
}
public static boolean isGreaterThan (int a,int b){
boolean isGreaterThan;
if(a>b){
isGreaterThan=true;
}else{
isGreaterThan=false;
}
return isGreaterThan;
}
}

javap -c FunctionDemeo 字节码详解
public static void main(java.lang.String[]);
Code:
0:bipush 10 //将10置于栈顶
2:istore_1 //将栈顶 int 型数值存入第二个局部变量。即a=10;
3:bipush 20 //将20置于栈顶
5:istore_2 //将栈顶 int 型数值存入第三个局部变量。即b=20;
6:iload_1 //将第二个int 型局部变量的值推送至栈顶 10
7:iload_2 //将第三个 int 型局部变量的值推送至栈顶 20
8:invokestatic #2 //Method isGreaterThan:(II)Z 调用静态方法(10,20)
11:istore_3 //将栈顶 int 型数值存入第四个局部变量
12:getstatic #3 //获取指定类的静态域,并将其值压入栈顶。System.out 放在栈顶
15:iload_3 //将第四个 int 型局部变量推送至栈顶。
16:invokevirtual #4 // out.println();调用对象的实例方法
19:return //方法结束

public static boolean isGreaterThan(int,int);
Code:
0:iload_0 //将第一个int 型局部变量的值推送至栈顶 a=10
1:iload_1 //将第二个int 型局部变量的值推送至栈顶 b=20
2:if_icmple 10 //if(10<=20) 就跳转到第10条指令
5:iconst_1 //将 int 型 1 推送至栈顶。
6:istore_2 //将栈顶 int 型数值存入第三个局部变量。即isGreaterThan=true;
7:goto 12 //无条件跳转到第12条指令
10:iconst_0 //将 int 型 0 推送至栈顶。
11:istore_2 //将栈顶 int 型数值存入第三个局部变量。即isGreaterThan=false;
12:iload_2 //将第三个int 型局部变量的值推送至栈顶。即isGreaterThan的值
13:ireturn //从当前方法返回 int.

原创粉丝点击