IF_ICMPNE 是什么意思?

对于以下 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什么意思?I4I13I22


潇湘沐
浏览 642回答 2
2回答

喵喵时光机

这是javap -c为您的类生成的输出(javap是每个标准 JDK 附带的工具):Compiled from "ArtClass.java"public class ArtClass {&nbsp; public ArtClass();&nbsp; &nbsp; Code:&nbsp; &nbsp; &nbsp; &nbsp;0: aload_0&nbsp; &nbsp; &nbsp; &nbsp;1: invokespecial #1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Method java/lang/Object."<init>":()V&nbsp; &nbsp; &nbsp; &nbsp;4: return&nbsp; public boolean foo(int);&nbsp; &nbsp; Code:&nbsp; &nbsp; &nbsp; &nbsp;0: iload_1&nbsp; &nbsp; &nbsp; &nbsp;1: ldc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // int 3956681&nbsp; &nbsp; &nbsp; &nbsp;3: if_icmpne&nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp; &nbsp;6: iconst_1&nbsp; &nbsp; &nbsp; &nbsp;7: ireturn&nbsp; &nbsp; &nbsp; &nbsp;8: iload_1&nbsp; &nbsp; &nbsp; &nbsp;9: ldc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // int 9855021&nbsp; &nbsp; &nbsp; 11: if_icmpne&nbsp; &nbsp; &nbsp;16&nbsp; &nbsp; &nbsp; 14: iconst_1&nbsp; &nbsp; &nbsp; 15: ireturn&nbsp; &nbsp; &nbsp; 16: iload_1&nbsp; &nbsp; &nbsp; 17: ldc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // int 63085561&nbsp; &nbsp; &nbsp; 19: if_icmpne&nbsp; &nbsp; &nbsp;24&nbsp; &nbsp; &nbsp; 22: iconst_1&nbsp; &nbsp; &nbsp; 23: ireturn&nbsp; &nbsp; &nbsp; 24: iconst_0&nbsp; &nbsp; &nbsp; 25: ireturn}所有指令的含义已在Java® 虚拟机规范的“指令集”一章中指定。if_icmpne指令会弹出两个int值,c o mp是它们,如果 不相等则跳转到指定的目标。的输出javap非常清楚,分支指令指定了哪些目标,因为它们与每条指令之前打印的数字相匹配。如果您使用不同的工具产生不同的输出,您必须参考该工具的文档,了解如何破译输出。与javap' 的输出进行比较表明,这些前缀 likeI4也指字节码偏移量,但没有进一步的上下文,例如查看该方法的其他指令,这是毫无意义的。

慕容森

这里有一个文档:http ://homepages.inf.ed.ac.uk/kwxm/JVM/if_icmpne.htmlif_icmpne:Description:&nbsp;jump&nbsp;to&nbsp;label&nbsp;if&nbsp;the&nbsp;two&nbsp;integer&nbsp;refs&nbsp;are&nbsp;not&nbsp;equal
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java