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

Wrapper Class '=='

Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Dear All,

I have noticed that the following code when executed produces output: GOT ==. This I understand is because '==' return true for Integer instances if the primitive value is between 127 and -128.



But the same code if changed as the following does not pass the 'if' test:


Why is this so? Integer.valueOf() method returns an Integer object instance right? What is the difference between the 2?
Please help!


SCJP, SCWCD, SCBCD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Jisha Anand wrote:Why is this so? Integer.valueOf() method returns an Integer object instance right? What is the difference between the 2?
Please help!

Yes, but note that there are two versions of the valueOf() method in class Integer: one that takes an int, and one that takes a String. Only the version that takes an int makes use of the internal cache that contains Integer objects for all values between -128 and 127. The version that takes a String always creates a new Integer object. (This was discussed in another topic recently, and I was surprised that it works like this - but it really does, you can have a look at the source code of class Integer which you will find in src.zip in your JDK installation directory to see it yourself).

So:
  • Integer.valueOf("-128") creates a new Integer object.
  • Integer smallS = -128 uses autoboxing, which means the compiler automatically translates it to: Integer smallS = Integer.valueOf(-128). Note that this uses the int version of the valueOf() method.
  • You now have to separate Integer objects, and comparing them with == will give false because == compares references (not values) when used on objects.


  • Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Jisha Anand
    Ranch Hand

    Joined: Apr 02, 2009
    Posts: 62

    Thanks a lot Jesper! Got it!

    It's the same as with Strings right:



    Just out of curiosity; why this discrimination? Is there any reason why the integer objects created by passing String doesn't get the benefit of internal cache?

    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14274
        
      21

    It's sort of the same as with strings, but in the case of strings, the compiler itself handles them in a special way, while with class Integer, it's just the implementation of that class that does it (the compiler doesn't need to do anything special).
    Jisha Anand wrote:Just out of curiosity; why this discrimination? Is there any reason why the integer objects created by passing String doesn't get the benefit of internal cache?

    I don't know, if you look at the String version of the valueOf() method, it looks like this:

    It would have been better if it was like this, so that it would take advantage of the cache:

    The JDK itself isn't perfect...
    Jisha Anand
    Ranch Hand

    Joined: Apr 02, 2009
    Posts: 62

    Jesper Young wrote:The JDK itself isn't perfect...


    true! Thanks a lot Jesper! That clarifies my doubt as well as curiosity
     
     
    subject: Wrapper Class '=='