aspose file tools*
The moose likes Java in General and the fly likes binary operator associativity and precedence order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "binary operator associativity and precedence order" Watch "binary operator associativity and precedence order" New topic
Author

binary operator associativity and precedence order

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

&& has higher precedence than ||, so A || B && C is A || (B && C).
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

Jeff Verdegan wrote:&& has higher precedence than ||, so A || B && C is A || (B && C).


thank you so much Jeff !
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 08, 2010
Posts: 1505
    
    5

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38353
    
  23
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Dec 08, 2010
Posts: 1505
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: binary operator associativity and precedence order