Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Internally Integer.valueOf is used for autoboxing.

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21203
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that he meant the other line. It doesn't get a reference of j. It gets it's value.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic