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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator precedence" Watch "operator precedence" New topic
Author

operator precedence

sanjay kumar
Greenhorn

Joined: Aug 29, 2006
Posts: 2
I am new to scjp preparation group. so first hi to all..

iam not absolutely clear about operator precedence .

question:
class Sam
{
static boolean a,b,c,d;
public static void main(String arg[])
{
d = (a=true) || (b=true) && (c=true);
System.out.println(a +","+b+","+c);
}
}

output:true,false,false
doubt:&& has higher precedence than || then why || is evaluated first.

suggest me a clear guide/tutorial for operator precedence and associativity.
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241
So the answer to your question goes as below

Evaluate: ((a=true) || ((b=true) && (c=true)));
Evaluate: ((true) || ((b=true) && (c=true))); // a=true is executed

As it knows true || something is always true (short circuited), the control breaks at this step without executing the further statements for b and c

So the result is a=true b=false and c=false
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Java Operator Precedence and Associativity Table
What are operator precedence and the order of operand evaluation?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: operator precedence