Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Precedence question!!!

 
Terence Lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder why the || operator is excetuted before && operator..:??
class A {
static boolean a;
static boolean b;
static boolean c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}
The result is true, false, false.
P.S This question is from Dan's mock exam, they are really helpful, thanks, Dan.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch, a friendly place for Java greenhorns
Please check out the following discussion which handles the same topic: http://www.coderanch.com/t/240790/java-programmer-SCJP/certification/Simple-precedence
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by TTT^O^TTT ^O^:

P.S This question is from Dan's mock exam, they are really helpful, thanks, Dan.

You're welcome.
That question seems to be a popular one here at the ranch.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by TTT^O^TTT ^O^:
I wonder why the || operator is excetuted before && operator..:??
class A {
static boolean a;
static boolean b;
static boolean c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}
The result is true, false, false.
P.S This question is from Dan's mock exam, they are really helpful, thanks, Dan.

The question is not why, it is why not. || and && has equal precedence, so it start from left to right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic