When Java evaluates a conjunction (<boolean exp1> && <boolean exp2>), does it eval exp2 if exp1 is false?

六眼飞鱼酱① 提交于 2019-11-28 02:05:36

From the Java Language Specification, 15.23 Conditional-And Operator &&:

The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

So the language spec guarantees that the right-hand side of your expression will not be evaluated if the left hand side is false.

No, java uses Short circuit evaluation. If expr1 is false, expr2 will not be evaluated, thus your && usage is perfectly safe.

Also, if you have if (exp1 || exp2) { .. } - exp2 will not be evaluated if exp1 is true.

Let us perform our own experiment by looking directly at the opcodes that are generated from this sample code:

public class Compare {

        public static void main(String... args) {
          boolean t = true;
          boolean f = false;
          if(f && t) {
            System.out.println("Both true");
          }
          else {
            System.out.println("One false");
          }
        }

}

javap -v generates:

   0:   iconst_1
   1:   istore_1
   2:   iconst_0
   3:   istore_2
   4:   iload_2
   5:   ifeq    23
   8:   iload_1
   9:   ifeq    23
   12:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   15:  ldc #3; //String No
   17:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   20:  goto    31
   23:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   26:  ldc #5; //String Yes
   28:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   31:  return

The relevant opcodes are ifeq for my small program. They check to see if the variables are equal to 0, and jump a certain number of operations forward if they are, in this case, to opcode 23. So if the first ifeq evaluates to false it will jump past the second ifeq instruction straight to the else statement.

This is called short circuit evaluation.

If you use && or ||, java will use short-circuit evaluation (ie not evaluate the second expression unless it needs to)

If you use & or |, java will always evaluate the second expression, even if the first was true

That's safe, Java does short circuit evaluations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!