Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing, == and equals 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, == and equals" Watch "Boxing, == and equals" New topic

Boxing, == and equals

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266

My doubt here is how do we get "different object" for i1 and i2 and "same object" for i3 and i4. Could someone please explain this ?
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
When I give this :

It doesnt enter the print statement.

When I give this :

It enters the print statment.

When I give this :

or this:

It enters the print statement

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

always remember that
"==" determines whether the reference variables refer to the same object
i.e. if Type obj = new Type();
Type obj1;
obj1 = obj;

then both the reference variables refer to the same object Type(); isn't it?
now in our situation,
Integer i1 = 1000;
Integer i2 = 1000;
we have two different objects having two different reference variables, hence if (i1 != i2) then println statement gets executed

and remember that equals method checks for the content of the object
now here
the content is same i.e. 1000 and 1000
hence i1.equals(i2) is true hence the meaningfully equivalent gets executed

But there is something weird with java, the equality depends upon the range of values we are providing
Read and remember following carefully :
Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105

Here i1 and i2 are created through Boxing and Integers created through boxing in the range -128 to 127 always pass the == test if their values are equal. Here i1 and i2 are greater than this range hence they pass != test. You make i1 and i2 smaller than this range and then
(i1!=i2) will return false

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

If the actual value of Integer is below 128 then it goes to literal pool like String literals but the values greater then 127 are always placed at heap. Whatever is at pool that will be shared by all the instances. so

will be placed on literal pool and will share the same copy. Hence == will be evaluated to true.

And this works for direct assignments only. Objects created using new Integer() are always placed at Heap.
Kaspar Christenson

Joined: Feb 26, 2009
Posts: 21

This is a really funky topic. Where in the specification is this clarified?

And got:

The key difference must be that the autoboxing is setting the two reference to the same object in the second case; whereas, in the first case they are indeed two different objects (Duh!).
Kaspar Christenson

Joined: Feb 26, 2009
Posts: 21

I've answered my own question. In section "5.1.7 Boxing Conversion" of the specification is states:
If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
I agree. Here's the link:
subject: Boxing, == and equals
It's not a secret anymore!