• 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, ==, equals(). Total confusion

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I am a bit confused about the 2 below code snippets:
Integer i1 = 1000;
Integer i2 = 1000;
if (i1 != i2) {
System.out.println("different objects");
}
if (i1.equals(i2)) {
System.out.println("meaningfully equal");
}

OUTPUT: different objects
meaningfully equal

Integer i3 = 10;
Integer i4 = 10;
if (i3 == i4) {
System.out.println("same objects");
}
if (i3.equals(i4)) {
System.out.println("meaningfully equal");
}

OUTPUT: same objects
meaningfully equal

I want to know how can this be possible? Both should use the same logic. Two wrapper objects can't be different and same at the same time.
Thank you for your help.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. This is confusing.
For all Integer with values < 128 , those Integer instances work just like primitive. So, all those values can be compare by the == sign.
But all Integer with values >=128, they are treated as object. If the references refer to the same object, then " i1==i2" is true; otherwise, false.
Of course , when their values are the same, using .equal methods will return true.

For more detail, please refer to autoboxing in KB's book.
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jia,

Class Integer defines a nested static class - IntegerCache. By default, it creates an array of Integer objects with values -128 to 127. If you try to create an object of class Integer within this range, your object reference refers to the existing Integer object. In this case, class Integer doesn't creates a new object. This is the reason why the object references refer to the same Integer object for values -128 to 127, and return true when compared for equality using the operator ==.

For your reference, here's the code of class IntegerCache defined within class Integer:



With respect,
Mala
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Jia. Please use the code button to post code snippets while asking questions. It makes the code much more legible.
 
Ranch Hand
Posts: 63
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more detail :
Check JLS Specification
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== and != in Integer literals adds a bit more to the subject...

Regards,
Dan
 
Jia Ramharai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, if I understand well,
when the Integer is between -128 and 127 then the result is true for i1 == i2 but
when the Integer is outside of the range above, then the result is false for i1 == i2 because here there are two Integer objects that have been created.

Thank you all for your replies. :-)
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, very confusing! I certainly hope this is not on the exam!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic