wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes short-circuit logical operator mystery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "short-circuit logical operator mystery" Watch "short-circuit logical operator mystery" New topic
Author

short-circuit logical operator mystery

Xander Steinmann
Greenhorn

Joined: Sep 18, 2007
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

Joined: Sep 06, 2007
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.


Author, Spring Security 3 (the Book), Packt Publishing, 2010
SCJP, OCP
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rakesh Yelugoila
Greenhorn

Joined: Jul 30, 2007
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

Thanks in advance.
Peter Mularien
Author
Ranch Hand

Joined: Sep 06, 2007
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

Joined: Sep 18, 2007
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: short-circuit logical operator mystery
 
Similar Threads
Operator Precedence involving && and ||
Bitwise & and | Siera Bates
Unable to understand result of this code
if condition problem ( ex from k&b )
Question by Sreenivasa Kumar Majji