This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes unboxing, object reference refer to heap vs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "unboxing, object reference refer to heap vs "class reference"" Watch "unboxing, object reference refer to heap vs "class reference"" New topic
Author

unboxing, object reference refer to heap vs "class reference"

wei liu
Ranch Hand

Joined: Nov 19, 2008
Posts: 35

Can someone help me to understand this paragraph from SCJP book

SCJP sun certified programmer for java 6 exam 310-065 book page 606 in the exam watch

"Remember that unboxing can't convert a plain old object to a primitive, even if that object reference refers to an integer(or some other wrapped primitive) on the heap.
unboxing converts onoly from a wrapper class reference (like an Integer or Long) to primitive.
"

I am quite confused about the concept between object reference refers to an integer on the heap vs unboxing converts onoly from a wrapper class reference (like an Integer or Long) to primitive., Can someone provide some examples to help me understand the difference between them.


thanks in advance


SCJP 1.6 in 2010
SCWCD5 in 2011
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Means unboxing only works if you are having an object of some wrapper class like :



Here i is a wrapper class and we can directly assign i to and integer (primitive) variable.

But this will not work if you have an object of Object class which is referring to an object of Wrapper class like:


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: unboxing, object reference refer to heap vs "class reference"
 
Similar Threads
Autoboxing vs Unboxing
SCJP problem with boxing
Autoboxing help
Boxing,autoboxing,unboxing
K & B, SCJP 5 - Errata: Updated 7/28/06