This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes question about wrap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about wrap" Watch "question about wrap" New topic
Author

question about wrap

xu han
Greenhorn

Joined: Nov 25, 2012
Posts: 7

sorry for my poor english at first.


/* Output:
false
true
*///:~
The operators == and != compare object references, so the output is actually “false” and then “true.”
I changed the code like this:

the output are both true.
if Integer n1=47;like Integer n1 = new Integer(47); create an object. n1 and n2 have different reference ,why n1==n2 is true?
because sentence (Integer n1) define n1 as a Integer class.so it can use method equals() to compare n1 and n2 .what does == compare?
what's the different of (Integer n1=47and (Integer n1= new Integer(47)?does the second sentence create an object? and the first one does not?
Thanks!



David Pacsuta
Greenhorn

Joined: Nov 24, 2012
Posts: 8

Hi!

If I were you, I'd UseCodeTags
xu han
Greenhorn

Joined: Nov 25, 2012
Posts: 7
Thanks for reminding.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
It's because certain classes like Integer have an object cache for common values.

If you use new Integer(..) then you are requesting a new object but if you use Integer.valueOf(..) or, as you have done, let the compiler provide the conversion from int to Integer then you may get a cached object and hence they are the same.
xu han
Greenhorn

Joined: Nov 25, 2012
Posts: 7
thanks for your help!
if the code like this:
Integer n1=47;
the compile just create a cache to hold the value.no object and reference has been create.the compiler treat n1 and n2 as two primitive type .so n1==n2 is true.
but it can still use method equals to compare their value?
am i right?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
No, the Integer has a cache that may contain an instance of the Integer with the value you want (ie 47), if it does you get the cached object reference if not you get a new instance object reference. There is no set defined range of values that are cached but on my machine using jdk1.6 on Windows the range is -128 to 127.

[Edited to correct typo in cache range]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Welcome to the Ranch

It is a common pitfall that wrapper objects are cached. You can find more details here, and maybe more links.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

And as long as you always remember to use the equals() method to compare Integer objects you don't need to worry about whether values are cached or not.
xu han
Greenhorn

Joined: Nov 25, 2012
Posts: 7
Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about wrap
 
Similar Threads
objects
Object comparison
comparing objects
equals method .. comparing objects
Doubt on equals()