File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Classes  Autoboxing Concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Classes  Autoboxing Concept" Watch "Wrapper Classes  Autoboxing Concept" New topic

Wrapper Classes Autoboxing Concept

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316

Output :

Different Objects
Meaningfully Equivalent Objects
Different Objects
Same Objects

Please Explain !!! < I think it is flaw in JAVA >

Take care

SCJP 6.0 96%

(Connecting the Dots ....)
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
Integer i3 = Integer.valueOf(12);
Integer i4 = new Integer("12");

i4 is created using new operator and so when you compare it with other wrappers they are not same(meaning they are different)

with new operator you are constructing a new object so with new opeartor you will be creating new objects which will not be the same even if those 2 objects are constructed to hold same primitive values.. Do trial and error method while coding;; you will get to know..

but i5 and i6 are same as (check K&B for assistance)

i am just a learner ;; i hope you get a better view of this concept from experts;;;

Thanks & Regards,
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Here is the explanation from K&B book (Chapter#3)

In order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same:
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127

true : true
false : true

Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
I agree. Here's the link:
subject: Wrapper Classes Autoboxing Concept
It's not a secret anymore!