my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Autoboxing 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 "Autoboxing" Watch "Autoboxing" New topic
Author

Autoboxing

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
hi,

Acc to my understanding, all primtives stay in stack, and objects stay in heap.Now, i want to know that how does int primitive is converted into Integer object in autoboxing. (i.e how does it get a reference to a stack and creates an object in the heap ). This was asked in one of the interviews which was attended by me ... Probabaly he wanted to know how does auutoboxing occurs internally

Also, he asked how can one improve/increase stack/heap memory programitaclly


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Internally Integer.valueOf is used for autoboxing.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
final int j = 5;


I would like to know how does it get reference of j, which resides in stack memory and then store it in heap. I would like to know w.r.t where it is stored etc i.e (Heap, Stack)...hope my question is clear
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18996
    
  40

Maan Suraj wrote:
final int j = 5;


I would like to know how does it get reference of j, which resides in stack memory and then store it in heap. I would like to know w.r.t where it is stored etc i.e (Heap, Stack)...hope my question is clear



Autoboxing doesn't occur with the code that you shown.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Assuming that he meant the other line. It doesn't get a reference of j. It gets it's value.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks wouter.... I meant the other line itself... :-)

so what i understand form your post is that , it reads the value from the primitive, create a new object, with the value as in primitive and stores it in a heap. Right?

Thanks Again
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yes that is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing