This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Precedence" Watch "Precedence" New topic
Author

Precedence

achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
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

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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Precedence
 
Similar Threads
&& and || operator precedence
boolean x = (a = true) || (b = true) && (c = true);
Dan's || && question
operator precedence
Logical operators (&& and ||)