• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Boolean values

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source :Sreenivasa Kumar Majji's Questions javaranch(FAQ)

some body please help me with following question:

[B]Q what is the output




answer is D) False

I thought it would be true due to auto boxing.I ran the code and it is coming FALSE only.I know i am missing something somewhere.

When I run the following code:
[/B]
output is True

In code below there is no output
[B]

Doesn't auto boxing applies to boolean.

Regards
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you are comparing two object references using == operator, it checks whether they are pointing to same object or not.



In above two cases you are creating two objects using new operator so it always gives always false as output,

In below case


you are comparing object with primitive so un boxing happens here and object will be converted to primitive that is why result of comparison is true
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check also page 236 in the SCJP 1.5 study guide.
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your efforts

I got the point.

Regards
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunny,

take a look at this example:



See that Integers between -128 and 127 initialzed with literal will always have the same reference, because java has a pool for it.

It works for Characters, Booleans, Longs, Integers, Shorts ands Bytes.
If i forgot any other, please, someone say please.
[ July 18, 2008: Message edited by: Raphael Rabadan ]
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

See that Integers between -128 and 127 initialzed with literal will always have the same reference, because java has a pool for it.

It works for Characters, Booleans, Longs, Integers, Shorts ands Bytes.
If i forgot any other, please, someone say please.




and ofcourse, the famous one - "Strings" ..
[ July 18, 2008: Message edited by: Sagar Rohankar ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Raphael Rabadan

Can you tell me why it is printing false for Integer i3 == Integer i4.
for i1==i2 it is printing true.can you clarify it.

regards
darshan
 
darshan karle
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry Raphael Rabadan

i didn't see the message you mentioned after the code.i am cleared

thanks
 
You got style baby! More than this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic