aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 36478
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: 19541

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
I agree. Here's the link:
subject: Wrapper Class
Similar Threads
Polymorphic Declaration
Error found in KB's pg236
How hashCode() works?
Sending mail through Java
problem about constructing Integer objects