excpp 2015-06-08 06:57 采纳率: 0%
浏览 1433

Java逻辑运算的字节码

测试代码如下:

 public class TestXLogic {

    public boolean xand (boolean b1, boolean b2) {
        return b1 && b2;
    }

    public boolean xor (boolean b1, boolean b2) {
        return b1 || b2;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        TestXLogic logic = new TestXLogic();

        boolean b1 = true, b2 = false;

        if (logic.xand(b1, b2) || logic.xor(b1, b2) && b2 ) {
            System.out.println("haha");
        }
    }

}

编译后,main方法里if条件部分是这样的:

 15  invokevirtual com.dicp.flow.test.TestXLogic.xand(boolean, boolean) : boolean [24]
    18  ifne 34
    21  aload_1 [logic]
    22  iload_2 [b1]
    23  iload_3 [b2]
    24  invokevirtual com.dicp.flow.test.TestXLogic.xor(boolean, boolean) : boolean [26]
    27  ifeq 42
    30  iload_3 [b2]
    31  ifeq 42
    34  getstatic java.lang.System.out : java.io.PrintStream [28]
    37  ldc <String "haha"> [34]

为什么18行会是ifne 34呢?实际上这里哪怕执行了15行,结果是true,但还需要运行后面&& b2才得到最终结果,而不应该是直接跳到34输出

  • 写回答

3条回答 默认 最新

  • Tiger_Zhao 2015-06-08 07:06
    关注

    || 是**短路**或运算,只要 xand() 为真就执行 println(),很正确啊。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器