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

Memory Spaces for primitive types and Objects

 
Greedy thomas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about memory allocation for primitive types and objects.
Suppose we have the following situation :
int x = 5 ;
AnObject singleObject = new AnObject() ;
Now x will be point to a place in memory and this place will store the value 5. However, for singleObject it will be allocated an address of where the object is stored in the heap i.e. singleObject points to an address.
Now if you have
int y = x ;
Then y will also point to what x is pointing to and thus also have the value 5.
i.e.
x --> 5 <-- y singleObject --> <address of singleObject>
Assuming my above explanation is correct, my question is :
Does the x, y and singleObject exist in the same memory space or does the address of where singleObject is stored come from another space.
Thanks In Advance,
Rajesh
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi R Mera
one thing i want to say about ur point of view is-
x--> 5 <-- y is not precise way of putting it.
if u change value of 'x' then 'y' value doesn't get change automatically as one might assume from the relation u show.
when we do int y=x; the "a memory space is created for an integer y and the same value as of current 'x' is given to it". BUT 'x' and 'y' are different variables. they DON'T point to same location.
about ur original question,
"objects" are allocated memory from heap. i'm not sure about the primitive types...
regards
maulin
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R,
You need to try and not think about "memory spaces" and crazy stuff like that. It's basically in the hands of the JVM that's running your program.
But .... to give you an idea (or a clarification) on your questions.
Inside some function, if you did "Object o = new Object()" then o would point to where the object is on the heap. (Actually, o would just contain some information that would allow the actual object on the heap to be found by the JVM when you need it.) So, o would kinda be like a pointer. Pointers in C/C++ would still be on the stack just like everything else.
So, the reference variable o would be on the stack. The actual object it "points to" or "uniquely addresses" is on the heap.
Primitives (like ints, longs, and the reference variables (not the objects they point to)) are all on the stack. They don't "point" anywhere. In fact, even using the word "point" in a discussion of objects on the heap can be misleading.
Think of the objects on the heap as people, and reference variables on the stack (that point to people) as names.
That help?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic