Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

binary operator associativity and precedence order

 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Experts !

I couldn't figure out how this line of code produced this output. Got a sec ? Much appreciated

System.out.println(true || true && false); returns true

Bug how come ??? I know that in Java 7 all binary operators, except asigment operators, are left associative, so normally JVM would so something like ((true || true) && false) which evaluates to (true && false) which evaluates to false.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& has higher precedence than ||, so A || B && C is A || (B && C).
 
Marius Constantin
Ranch Hand
Posts: 62
Java Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:&& has higher precedence than ||, so A || B && C is A || (B && C).


thank you so much Jeff !
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: && has higher precedence than ||

This is absolutely right, but in this particular example, other factor decides the output : short-circuit binary operators.

When there's an expression like : expr1 || expr2 <followed by a number of binary operations>, then, Java is intelligent enough to understand that anything orred with true will be true and it doesn't even bothers to evaluate further expressions.

So, when you say 'true ||', Java says, 'ok, that's gonna be true, so I don't care what is at right side of ||'.

If you anyway want those expressions to be evaluated, you can use '|' instead of '||'.

I hope this helps.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:
Jeff Verdegan wrote: && has higher precedence than ||

This is absolutely right, but in this particular example, other factor decides the output : short-circuit binary operators.


No, that doesn't affect it at all. Short-circuit operations, by definition, do not affect the outcome. They cannot (unless the affected part of the expression has a side effect), otherwise they would make for incorrect behavior.

Short circuiting doesn't decide the output. It only ends evaluation once the output (result of the expression, rather) has already been decided.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Constantin wrote:
Jeff Verdegan wrote:&& has higher precedence than ||, so A || B && C is A || (B && C).


thank you so much Jeff !


You're very welcome. And welcome to the Ranch!
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: otherwise they would make for incorrect behavior

No, I'm not saying it that way (perhaps I put it wrongly when I said 'other factor decides the output').

What I wanted to say is : due to short-circuit operators, the output will be decided (i.e. evaluation will stop) even before priority comes into picture. As you said, it simply decides when to stop evaluation.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:
Jeff Verdegan wrote: otherwise they would make for incorrect behavior

No, I'm not saying it that way (perhaps I put it wrongly when I said 'other factor decides the output').

What I wanted to say is : due to short-circuit operators, the output will be decided (i.e. evaluation will stop) even before priority comes into picture.


Sorry, still not right. Precedence makes it equivalent to
T || (T &&F)
but if && and || had equal precedence, or if || was higher, then it would be equivalent to
(T || T) && F
in which case we would have to evaluate the final F to know the value of the expression.

That is, it's only because of precedence that we can short-circuit this in the first place.

(Of course, in the second case, we would short-circuit the (T || T) part, but that's not what we're talking about.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . .
but if && and || had equal precedence, or if || was higher, then it would be equivalent to
(T || T) && F . . .
That is because those operators associate to the left.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Jeff Verdegan wrote: . . .
but if && and || had equal precedence, or if || was higher, then it would be equivalent to
(T || T) && F . . .
That is because those operators associate to the left.


For equal precedence, yes, that's why.

Irrespective of that, however, the statement that, "due to short-circuit operators, the output will be decided (i.e. evaluation will stop) even before priority comes into picture," is incorrect.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: That is, it's only because of precedence that we can short-circuit this in the first place.

Now I got it. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic