• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Class

 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference between these two below ? And which one is recommended ?
(1)
(2)

Thanks in advance
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That latter. That is because the value might already have been used, and you might save memory and object construction overhead by re-using the old object. This only works for immutable classes. The details are in the API documentation.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The former gets turned into the second one by the compiler. There is no difference between the two.

@Campbell: I think you're confused by using new Integer(250) versus Integer.valueOf(250). In that comparison you're absolutely right - valueOf is definitely preferred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic