aspose file tools*
The moose likes Beginning Java and the fly likes A doubt related to Wrapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A doubt related to Wrapping" Watch "A doubt related to Wrapping" New topic
Author

A doubt related to Wrapping

ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

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

Joined: Jun 06, 2011
Posts: 12
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

Joined: Jun 10, 2011
Posts: 207

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.


Piyush
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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

Joined: Mar 16, 2011
Posts: 62

Thanks guys.

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

https://www.owasp.org/index.php/Java_gotchas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A doubt related to Wrapping