aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator precedence" Watch "Operator precedence" New topic
Author

Operator precedence

sangeetha balasubramaniam
Greenhorn

Joined: May 16, 2005
Posts: 23
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

Joined: Aug 02, 2005
Posts: 167

a || b
if a is true, b is not evaluated.
so is the same above.
Balazs Borbely
Ranch Hand

Joined: Oct 11, 2004
Posts: 33
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.


'Make everything as simple as possible, but not simpler.' --Albert Einstein
Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
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.


Regards,<br />Samir (SCJP 1.4)
Kalyana Sundaram
Ranch Hand

Joined: Mar 18, 2005
Posts: 94
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 ]

Only those who will risk going too far can possibly find out how far one can go !!!
sangeetha balasubramaniam
Greenhorn

Joined: May 16, 2005
Posts: 23
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

Joined: Mar 14, 2003
Posts: 806
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


Thanks and Regards, Amit Taneja
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Good enough ..if u can give the reason for the behaviuor....

Tx
sangeetha balasubramaniam
Greenhorn

Joined: May 16, 2005
Posts: 23
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

Joined: Aug 02, 2005
Posts: 167

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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979


Well what does these terms mean

operator precedence

associativity

evalation order

Can you quickly summarize them for us???

Regards
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Refer to the JLS.

Or a book such as The Java Programming Language - third edition.
[ September 02, 2005: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator precedence
 
Similar Threads
about logical AND , OR
Conditional Operators
operator precedence
Dan's || && question
boolean x = (a = true) || (b = true) && (c = true);