• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A doubt related to Wrapping

 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through the Auto-boxing topic of Kathy Sierra SCJP 6 (page 246). It says that if we try to compare 2 instances of wrapped objects, the objects are unwrapped and their values are compared.

//----- Code Snippet 1 (Wrapping without using 'new')
Integer i1 = 100;
Integer i2 = 100;

if(i1.equals(i2))
System.out.print("1");

if(i1==i2)
System.out.print("2");

The output is : 1 2


//Code Snippet 2 (Wrapping using 'new')
Integer i3 = new Integer(200); // can we call this wrapping/boxing ?
Integer i4 = new Integer(200);

if(i3.equals(i4))
System.out.print("3");

if(i3==i4)// is this comparing instances and not values ?
System.out.print("4");

The output is : 3


Why the Code Snippet 2 , does not prints "4". Why its not comparing the values of the Wrapped instances.
 
Dan Din
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashwin.


In the 2nd snippet, you can try to put



and you got the output 4.
The idea is that all Integer object with values between -128..127 are immutable, so the references are the same.


Hope that helps.

[le]
Sorry, is wrong what i was saying...in the 2nd snippet you have 2 object references who are not equals, so nothing to do with immutability or values...

To test the immutability, you can change the values from 1st snippet (make them 200) - then, you'll see that i1 and i2 are 2 different references and i1 == i2 evaluate to false
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashwin bhawsar wrote:I was going through the Auto-boxing topic of Kathy Sierra SCJP 6 (page 246). It says that if we try to compare 2 instances of wrapped objects, the objects are unwrapped and their values are compared.

That's wrong. The book says : When == is used to compare a primitive to a wrapper, the wrapper will be unwrapped and the comparison will be primitive to primitive.

I have compiled some rules the comparison of wrapper objects:

Integer i1 = 1000; //this is boxing
Integer i2 = 1000; //this is boxing
Integer i3 = 10; //this is boxing
Integer i4 = 10; //this is boxing
Integer i5 = new Integer(10); // this is not boxing
Integer i6 = new Integer(10); // this is not boxing

Compare Using Compare With Result
i1 == i2 false
i3 == i4 true
i5 == i6 false
i1 .equals() i2 true
i3 .equals() i4 true
i5 .equals() i6 true


Compare row 1,2 and 3 it looks confusing at first sight, as it was for me.

If you create a Wrapper object using boxing then its constructor will not be called, but valueOf() method will be called.
If you check the javadocs for this method, it says that this method caches frequently requested values.
Generally caching is done for:
1. Short and Integer from -128 to 127
2. Character from \u0000 to \u007f
3. Byte
4. Boolean

So if you have Integer in this range then you won't get two different objects when you write Integer i3=10; Integer i4=10;. Therefore i3==i4 is true and i1==i2 false.

 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SearchFirst. This question has been asked many, many times before.

Piyush is right about the caching and the ranges, but with two additions:
1) Long also has the cache for values from -128 to 127.
2) the upper bound for Integer can be increased using a specific system property (which I can't remember right now).
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
2) the upper bound for Integer can be increased using a specific system property (which I can't remember right now).


I searched for the name of property, and it is java.lang.Integer.IntegerCache.high
 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

I have also found a good link that explains this topic.

https://www.owasp.org/index.php/Java_gotchas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic