Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Logical 'AND' vs 'OR' Short-Circuiting Consistency [duplicate]

Possible Duplicate:
Difference in & and &&

I've read several tutorials and answer regarding short circuit operations in java and I'm still not quite understanding the difference in how java handles short circuiting for a double vertical pipe vs a double ampersand. For instance ...

Why does the logical AND short circuit evaluation fail?

Citing the JSL 15.23. Conditional-And Operator &&

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 );

    }

}
like image 236
Edward J Beckett Avatar asked Dec 04 '22 13:12

Edward J Beckett


2 Answers

I don't understand. Shouldn't the Short Circuit succeed since the left side of the equation equals 1?

No, absolutely not. The point of && is that the result is only true if the left and the right operands are true; the short-circuiting means that the right operand isn't evaluated if the left operand is false, because the answer is known at that point.

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

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.

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

like image 125
Jon Skeet Avatar answered Jan 18 '23 23:01

Jon Skeet


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

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

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

This is what we mean by "short-circuiting": the process of leaving the second argument unevaluated because in certain cases we can know the value of the whole expression just from the value of the first argument.


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

like image 21
arshajii Avatar answered Jan 19 '23 00:01

arshajii