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

Logic below not followed

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
What is the output of the following code?

Monkey

I did not understand this logic. Please explain

Ranch Hand
Posts: 79
• 2
• Number of slices to send:
Optional 'thank-you' note:
Hi,

I just added two print statements:

here is the output:

boo2==>false
boo3==>true
Monkey

The below concepts would help to understand the output:

Default Values:

In this class, the variable 'boo2' is not initialized. If the member and static variables are not initialized, they would get default values. example, booleans get a default value of false, integers get a default value of 0.

Hence the value of 'boo2' is false in this example

instanceof operator:
This operator is used to check whether an object falls under certain type. Every object inherits the class 'Object'. So the variable boo3 is set to true

Order of precedence:

& operator has higher precedence than the | operator.

So, (boo1 & boo2 | boo2 & boo3 | boo2 ), after substituting the values will be like below:

( true & false | false & true | false ) will result in
( false | false | false ) will result in
( false )

Similarly, ( boo3 & boo2 | boo2 & boo1 | boo2 | boo1) will result in

( true & false | false & true | false | true )
( false | false | false | true )
(true)

Hope this helps,

Regards