• 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()

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard,
Thanks for the explanation . Its clear now.
Kind Regards.
Hasnain Javed Khan.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic