• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

logical operator precedence question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in sun's web site, it states '&&' has higher precedence than '||'
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

but the following code doesn't indicate so, why?

the output is:
true
3
 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jia,

In this example you can see the code System.out.println(true || true && ++a == 3);
As we have studied, the operation always takes place from left to right. So when a true is encountered before the || condition, it directly comes out and moves to the next line of execution. If a false was encountered before the ||, then only the right hand side of the logical OR operator will be evaluated.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An operator with higher precedence will associate with its operands first, but that doesn't mean the operator is executed first:
true || true && ++a == 3
is the same as:
true || (true && ++a == 3)
This will then evaluate the left operand of ||, which is true, and since || is a short circuit operator, the right operand will never be evaluated.

In contrast,
true && true || ++a == 3
is the same as:
(true && true) || ++a == 3
This won't increment a either though.

Also, notice that if || had higher precedence than &&, then
true || true && ++a == 3
would be the same as:
(true || true) && ++a == 3
In that case, a would be incremented and become 4.
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben, that explain is very clear.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good to know, Jia!
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic