jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing, ==, equals(). Total confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing, ==, equals(). Total confusion" Watch "Boxing, ==, equals(). Total confusion" New topic

Boxing, ==, equals(). Total confusion

Jia Ramharai

Joined: Feb 21, 2013
Posts: 5
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.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 1114
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.
Mala Gupta
Ranch Hand

Joined: Sep 27, 2002
Posts: 280

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,

Author of Manning's OCA Java SE 8 Programmer I Certification Guide, OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the Ranch Jia. Please use the code button to post code snippets while asking questions. It makes the code much more legible.

~ Mansukh
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

For more detail :
Check JLS Specification
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1183
== and != in Integer literals adds a bit more to the subject...


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Jia Ramharai

Joined: Feb 21, 2013
Posts: 5
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. :-)
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
yes, very confusing! I certainly hope this is not on the exam!
I agree. Here's the link: http://aspose.com/file-tools
subject: Boxing, ==, equals(). Total confusion
It's not a secret anymore!