Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
wei liu
Ranch Hand
Posts: 35
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic