Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • 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
Marshal
Posts: 52663
121
  • 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
Posts: 20822
68
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!