• 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

Boxing and equals operators

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

Here is the code:



Here is the output when I run it:


i1 and i2 are the same according to ==
i1 and i2 are the same according to .equals



In the SCJP 6 (KS&BB) book on page 245 it says that i1 != i2 should evaluate to true, which is not the case when I run it.

Can anyone please explain what the expected behavior should be?
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Slobo,

Welcome to JavaRanch

 
Slobo Sljivo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nihad,

I have joined JavaRanch a year ago when I first attempted to do the certification. I have gone through half of the book back then and gave up. I am determined to make it to the end this time

Cheers,

Slobo
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Slobo", please check your private messages for an important administrative matter.
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Slobo,

Always keep in mind that objects created by autoboxing will go into constant pool created by JVM when the integer value falls in the range between -128 to 127 otherwise objects are created on heap.
The objects created using new always goes on heap so each object have different memory location even if thay have same integer value and falls in the range between -128 to 127. so == operator evaluates to false.

I modified your code for explaination purpose.
See the code given below.





Ouput is given below

i1 and i2 are the same according to ==
i1 and i2 are the same according to .equals

i3 and i4 are the same according to ==
i3 and i4 are the same according to .equals

i5 and i6 are not the same according to !=
i5 and i6 are the same according to .equals

i7 and i8 are not the same according to !=
i7 and i8 are the same according to .equals

i9 and i10 are not the same according to !=
i9 and i10 are the same according to .equals

i11 and i12 are not the same according to !=
i11 and i12 are the same according to .equals



Also do a seach this forum on "autoboxing","constant pool" you will get autoboxing and comparison of autoboxed object explained many times before.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Slobo Sljivo wrote:
Can anyone please explain what the expected behavior should be?



see here, the link might help you.


 
Slobo Sljivo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ninad,

I got it now. Thanks for perfect answer.

Lessons learned: I will search the forum bit more before I post question next time.

Cheers,

Slobo
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question related to autoboxing/unboxing that's been bugging me for some time.

Could someone please explain the mechanism behind the foloowing code?

boolean d = (new Integer("1123")==1123d);
System.out.println(d); => TRUE

Cheers,
Horia
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Horia, welcome to javaranch.

Please start a new topic for your problem by clicking .
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic