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

Precedence

 
achayya matta
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
My question is which of the following operators has highest precedence?
a). &&
b). ||

Which operator gets first evaluated when both are present in an expression?

in Khalid-Mughal book he has shown && operator having highest precedence than ||.

According to Khalid,the output of the following code should be "false,true,true" but i am getting "true,false,false"(jdk1.4.2_03).
can u tell me why so ?

class EBH202 {
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);
}}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true, false, false is correct.

Rewriting the expression with parentheses according to operator priority gives:

( a = true ) || ( ( b = true ) && ( c = true ) )

The first thing executed is the a = true assigment which also evaluates to true. Because || is a shortcut operator, the sub-expression ((b=true) && (c=true)) is not executed, therefore b and c remain at their defaulted values of false.

So a is true, b and c are false.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic