Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about wrap

 
xu han
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

If I were you, I'd UseCodeTags
 
xu han
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reminding.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2957
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic