File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator precedence

 
sanjay kumar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Operator Precedence and Associativity Table
What are operator precedence and the order of operand evaluation?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic