• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

short-circuit logical operator mystery

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic