Java Logical AND and OR Short Circuit

Possible duplicate:
Difference in &&&

I read several guides and answers regarding short circuit operations in java, and I still don't quite understand the difference in how java handles a short circuit for a double vertical pipe compared to a double ampersand. For instance...

Why does a logical AND short circuit error fail?

JSL Quoting 15.23. Conditional and operator & </p>

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

 public static void main( String... args ) { int a = 1; int b = 2; // Okay. Prints if( a == 1 | b == 3 ) { System.out.println( "Comparison via |" + "\na is " + a + "\nb is " + b ); } // Okay. Prints if( a == 1 || b == 3 ) { System.out.println( "Comparison via ||" + "\na is " + a + "\nb is " + b ); } // Okay. Does not print if( a == 1 & b == 3 ) { System.out.println( "Comparison via &" + "\na is " + a + "\nb is " + b ); } // I don't understand. Shouldn't the Short Circuit succeed since the left side of the equation equals 1? if( a == 1 && b == 3 ) { System.out.println( "Comparison via &&" + "\na is " + a + "\nb is " + b ); } } 
+4
source share
5 answers

I do not understand. Shouldn't the short circuit succeed since the left side of the equation is 1?

No, absolutely not. The point && is that the result is only true if the left and right operands are true ; a short circuit means that the right operand is not evaluated if the left operand is false , because the answer is known at this point.

You should read sections 15.23 and 15.24 of the JLS for more details:

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

Conditional or statement || operator is like | (§15.22.2), but evaluates its right operand only if the value of its left operand is false.

+10
source

When used with boolean s, bitwise operators ( | and & ) are similar to logical operators ( || and && ), except that:

  • In the case of && , if the first argument is false , the second remains invaluable, because then the whole expression must be false . For & both arguments are evaluated independently.

  • In the case of || if the first argument is true , the second remains invaluable because then the whole expression must be true . For | both arguments are evaluated independently.

This is what we mean by “short circuit”: the process of leaving the second argument is invaluable, because in some cases we can know the meaning of the whole expression only by the value of the first argument.


Now you are asking why the following expression is false : a == 1 && b == 3 . Well, a short circuit has nothing to do with it; a = 1 and b = 2 , so the statement "a is 1 and b is 2 is obviously false , since b not 2 .

+2
source

Logical Operators || and && short circuit, if the result is determined after evaluating the first operand. For || this is the case if the first operand evaluates to true , for && if it evaluates to false .

If the first operand || is false , it can still give the overall result true if the second operand is true . Similarly, if the first && operand is true , it can still evaluate to false if the second operand is false .

In Java, operators | and & are not only bitwise operators (when applied to integer arguments), but also short-circuited logical operators that evaluate both operands regardless of the value of the first.

+1
source

Both and && require both sides to be true, so the code behaves as expected.

The only difference is that both sides perform, but && only performs the first if it is false, because the right side will not be related to the final result.

The effect of this is important for type code

 if (obj == null || obj.isSomthing()) 

will throw NPE if you used | and obj is null.

+1
source
 if( a == 1 && b == 3 ) { System.out.println( "Comparison via &&" + "\na is " + a + "\nb is " + b ); } 

At first it checks if a == 1 is true, so it should go on and check if b == 3 , it is not, therefore true && false is false and you will not get the output.

if you have

 if( b == 3 && a == 1 ) { System.out.println( "Comparison via &&" + "\na is " + a + "\nb is " + b ); } 

he will first check if b == 3 , and since it is not, don’t even look at a == 1 , because false && whatever always false , it doesn’t matter what.

Does this answer your question?

0
source

Source: https://habr.com/ru/post/1446571/


All Articles