测试代码如下:
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输出