• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator precedence

 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone help me in operator precedence for short-circuit and bitwise operator.

I found in net that && has more precedence than the ||, after executing the below code from (http://www.danchisholm.net) i really doubt it. I expected the answer to be true, true, true.

class BooleanTest{
static boolean a, b, c;
public static void main(String args[]){
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}

got answer as : true, false, false


Thanks in Advance,
Sangeetha
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a || b
if a is true, b is not evaluated.
so is the same above.
 
Balazs Borbely
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kudret Serin:
a || b
if a is true, b is not evaluated.
so is the same above.


Here is about a || b && c and && has a bigger precedence than || ... normally the b && c should be evaluated first.

Maybe the java implementation version is buggy.
 
Samir Dash
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kudret is correct.
Sangeetha, I am just refering the main line in your code.

Here you assignment and logical check operation occurring simaltaneously.
For || and && operator associativity is from left to right.
So here first a will be evaluated to true (now the first expression is true), then it will encounter the short circuit || operator and will not check further due to true value of first expression and hence you are getting the output as you mentioned.

You are also right that && has higher precedence than ||.

Hope this will clarrify your query.
 
Kalyana Sundaram
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys
I gotta quick fix for this


Just take out one "|"
So now we dont have Conditional OR, what we have now is just logical OR.
IN this situation, all the stuff in the paranthesis have to be evaluated strictly following the evaluation order, precedence rules, associativity, blah, blah...
The result must be true, true, true as expected by you.
[ September 01, 2005: Message edited by: Kalyana Sundaram ]
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kalyana you are right, when the short circuit operator is changed to bitwise, i do agree your answer.

But still i did not get the answer for operator precedence between && and ||. Please help me..
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forget everything
and remember only that . || and && have same level of precedence and
whenver || occurs rest of the code skiped

even its like this a||b&& c|| d
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good enough ..if u can give the reason for the behaviuor....

Tx
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No,
i want a valid reason, as why the above code not following the operator precedence..Please someone help me...



Thanks,
Sangeetha.
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found in net that && has more precedence than the ||, after executing the below code from (http://www.danchisholm.net) i really doubt it


Where did you find it?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have forgotten is that an expression is evaluated from left to right after the precedence grouping has been performed.

So:


is certainly grouped according to precedence as:

but the expression is then evaluated from left to right. That is, a is set to true, and the result of that assignment expression is true. The logical or (||) now short circuits because it already has a true argument. So the result of the whole expression is true and the assignments to b and c did not take place.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well what does these terms mean

operator precedence

associativity

evalation order

Can you quickly summarize them for us???

Regards
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to the JLS.

Or a book such as The Java Programming Language - third edition.
[ September 02, 2005: Message edited by: Barry Gaunt ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic