对于以下 Java 类:
public class ArtClass {
public boolean foo(int x) {
if(x == 3956681)
return true;
else if(x == 9855021)
return true;
else if(x == 63085561)
return true;
else
return false;
}
}
其JVM指令为:
I4 Branch 1 IF_ICMPNE L3
I13 Branch 2 IF_ICMPNE L5
I22 Branch 3 IF_ICMPNE L7
我知道第一个分支位于第三行,第二个和第三个分支也相同,但是 IF_ICMPNE
是什么意思,以及 I4
、 是什么意思? >I13
和 I22
是什么意思?
最佳答案
这是为您的类生成的输出 javap -c
(javap
是每个标准 JDK 附带的工具):
Compiled from "ArtClass.java"
public class ArtClass {
public ArtClass();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public boolean foo(int);
Code:
0: iload_1
1: ldc #2 // int 3956681
3: if_icmpne 8
6: iconst_1
7: ireturn
8: iload_1
9: ldc #3 // int 9855021
11: if_icmpne 16
14: iconst_1
15: ireturn
16: iload_1
17: ldc #4 // int 63085561
19: if_icmpne 24
22: iconst_1
23: ireturn
24: iconst_0
25: ireturn
}
所有指令的含义已在“Instruction Set” chapter of the The Java® Virtual Machine Specification中指定。 。指令if_icmpne将弹出两个int值,comp是它们,并分支到指定的目标if 不等于。
javap 的输出非常清楚地表明哪些目标已由分支指令指定,因为它们与每条指令之前打印的数字相匹配。
如果您使用不同的工具产生不同的输出,则必须引用该工具的文档,了解如何解读输出。与 javap
的输出进行比较表明,这些前缀如 I4
也指字节码偏移量,但没有进一步的上下文,例如看到该方法的其他说明,这毫无意义。
关于java - IF_ICMPNE 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54425043/