File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Boxing, ==, and Equals()

Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello All,
Could anyone kindly explain the following taken from K&B

Integer i1 = 1000;
Integer i2 = 1000;
if(il != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");

Produces the output:

different objects
meaningfully equal

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

This example produces the output:

same object
meaningfully equal

Yikes! The equals() method seems to be working, but what happened with == and != ? Why is != telling us that i1 and i2 are different objects, when == is saying that i3 and i4 are the same object? In order to save memory, two instances of the following wrapper objects will always be = = when their primitive values are the same:

Boolean

Byte

Character from \u0000 to \u007f (7f is 127 in decimal)

Short and Integer from -128 to 127

I'm lost

Waiting for a favorable reply.
Kind Regards.
Hasnain Javed Khan.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy ranchers!

If the object of a wrapper is within the size of a byte and you are boxing, only one objects is made and this goes into the wrapper's pool.
The other variable refers to the same object then and so == indicates that they are the same object.


For numbers extending the range of a byte such a pooling is not possible (remember - there are a lot of different numbers out there ).

But as I said already this works only with boxing.

So:

Integer a = new Integer(10);
Integer b = new Integer(10);
System.out.println(a==b); // false
Integer c = 10;
Integer d = 10;
System.out.println(c==d); // true
Integer big1 = 1000;
Integer big2 = 1000;
System.out.println(big1==big2); // false



prints what's indicated in the comments.


== only indicates if the objects are the same, not the value.
equals indicates if they are meaningfully equal. Would be all true with the example.


Will be back in january,
Bu.
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hi Burkhard,
Thanks for the explanation . Its clear now.
Kind Regards.
Hasnain Javed Khan.
V Black
Greenhorn

Joined: May 07, 2008
Posts: 6
In this section of the book(pg236) it is mentioned that this pooling holds true for the following wrapper classes:
Byte
Short
Integer
Boolean
Character

I notice Long is not mentioned

What about Long?- I find that the same pooling behavior happens for Long as well:

//testEquality is a method that uses the == operator
Long long1 = 10L;
Long long2 = 10L;
testEquality(long1, long2, "Longs(10) long1 and long2 are: ");

Long longer1 = 128L;
Long longer2 = 128L;
testEquality(longer1, longer2, "Longs(128) longer1 and longer2 are: ");

output:
Longs(10) long1 and long2 are: same object
Longs(128) longer1 and longer2 are: different objects

Is it that for Long, the pooling is not guaranteed?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also see our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boxing, ==, and Equals()