Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question on evaluation Order in S.o.p

 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx henry.....
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )



 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic