wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  A question on evaluation Order in S.o.p 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 " A question on evaluation Order in S.o.p" Watch " A question on evaluation Order in S.o.p" New topic
Author

A question on evaluation Order in S.o.p

ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
heres a question which got me confused about evaluation order in System.out.println.
What is the output of the following code?

public class BooBoo {
public static boolean boo2;
public static void main(String[ ] args)
{
boolean boo1 = true;
boolean boo3 = new BooBoo() instanceof Object;
if ( boo1 & boo2 | boo2 & boo3 | boo2 )
System.out.println("Donkey");
if ( boo3 & boo2 | boo2 & boo1 | boo2 | boo1)
System.out.println("Monkey");
}
}

the answer is Monkey..
plzzz somebody explain it....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Basically, the bitwise AND operator has higher precedence than the bitwise OR operator. So this set of code...



Is basically this....



Substituting the values of "boo1=true", "boo2=false", and "boo3=true"... just work that out... you get...



And once you work out the expression, you get false for the first condition, and true for the second condition.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
thnx henry.....
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Know this :

& and | will check both operands

static variables are always initialised to their default values and boolean's default is false..

now for your question :
if( boo1 & boo2 | boo2 & boo3 | boo2 ) will evaluate like :
___true & false | false & true | false which is false.. ( true & false is false, false & true is false, "or" ing false and false is false )

second if statement :
if( boo3 & boo2 | boo2 & boo1 | boo2 | boo1 )
__true & false | false & true | false | true which is true .. ( first two parts are false, but last "or" has a true value.. so since the whole thing is "or", it is true )




Regards
Shri..

SCJP 5.0
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
man.... Henry is so quick..
@Henry : Are you sitting in front of the computer hitting refresh every 15 seconds waiting for someone to post something ? I am really amazed at the speeds you find an open question..
Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 38

I was thinking the same... actually i thought it every 5 sec , but I find that very cool
Henry are you gonna tell us the secret ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Valentin Ivanov wrote: I was thinking the same... actually i thought it every 5 sec , but I find that very cool
Henry are you gonna tell us the secret ?


Dumb luck? I just happened to step inside at the right time.... Regardless, it wasn't 5 seconds. There was a 12 minute interval where it could have been answered first...
Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question on evaluation Order in S.o.p
 
Similar Threads
Logical operators precedence
Order of precedence & evaluation
operator precedence K&B question
how to solve expression ...
About Thread and String. Help!