This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper Class" Watch "Wrapper Class" New topic

Wrapper Class

Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Is there any difference between these two below ? And which one is recommended ?

Thanks in advance
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38472
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

Joined: Oct 27, 2005
Posts: 19679

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.

How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater.
subject: Wrapper Class