Win a copy of Learn Spring Security (video course) this week in the Spring forum!

# short-circuit logical operator mystery

Xander Steinmann
Greenhorn
Posts: 4
Hi,

After reading page 321 about combining logical operators without using (many) parentheses, I did some investigating:

In the book it says that line 7 returns false because the program puts parentheses around the rest. But why doesn't it do that at line 1? (and at lines 4 and 10?). It obviously has something to do with the non-shortcut operators but I haven't been able to figure out what exactly happens (and what would happen if I added another "&& false" or something like that at the end).

Can anyone solve this mystery?

Xander

Peter Mularien
Author
Ranch Hand
Posts: 84
System.out.println(false && true | true); //results in false !? (7)

Remember boolean operators are evaluated left to right, and boolean operators (&&, ||) are higher in precedence than binary logical operators (&, |). So the order of evaluation is:
* false
* true | true
Since the AND operator requires that both sides evaluate to "true", it evaluates the first operand (false) and then stops, because the expression would never evaluate to true (false AND anything will always be false). This isn't really a question of short-circuit evaluation, but instead a question of understanding operator precedence.

ahmed yehia
Ranch Hand
Posts: 424
I have a doubt in that since the OR "|" gets its operands executed first because it has higher precedence than AND "&&" so you can assume the code like that:

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Originally posted by Peter Mularien:
Remember boolean operators are evaluated left to right, and boolean operators (&&, ||) are higher in precedence than binary logical operators (&, |).

No; the bitwise operators & and | have higher precedence than the logical operators && and ||. The Java Tutorial: Operators has a table with the operator precedences.

So line 1: false && true || true is evaluated as (false && true) || true which is true
And line 7: false && true | true is evaluated as false && (true | true) which is false
[ September 18, 2007: Message edited by: Jesper Young ]

Rakesh Yelugoila
Greenhorn
Posts: 19
Originally posted by Jesper Young:

No; the bitwise operators & and | have higher precedence than the logical operators && and ||. The Java Tutorial: Operators has a table with the operator precedences.

So line 1: false && true || true is evaluated as (false && true) || true which is true
And line 7: false && true | true is evaluated as false && (true | true) which is false

[ September 18, 2007: Message edited by: Jesper Young ]

Can anyone explain why 6is FALSE? Coz if the unary operators have high precedence I assume the the expression is evaluated like

((true & true) || false)) = (T || F) = T

Peter Mularien
Author
Ranch Hand
Posts: 84
Originally posted by Jesper Young:

No; the bitwise operators & and | have higher precedence than the logical operators && and ||. The Java Tutorial: Operators has a table with the operator precedences.
[ September 18, 2007: Message edited by: Jesper Young ]

Thanks for the correction! One of the best ways to learn is by being corrected

Xander Steinmann
Greenhorn
Posts: 4
Can anyone explain why 6is FALSE? Coz if the unary operators have high precedence I assume the the expression is evaluated like

((true & true) || false)) = (T || F) = T

Sorry, line 6 should be true. my error

And thanks for the explanation everybody!

 Don't get me started about those stupid light bulbs.