• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question on evaluation Order in S.o.p

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx henry.....
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic