This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Memory Spaces for primitive types and Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory Spaces for primitive types and Objects" Watch "Memory Spaces for primitive types and Objects" New topic
Author

Memory Spaces for primitive types and Objects

Greedy thomas
Ranch Hand

Joined: May 15, 2003
Posts: 37
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: May 29, 2003
Posts: 1258
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?


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Spaces for primitive types and Objects
 
Similar Threads
Heap and Stack difference.
i++i+++;
pass by value vs pass by reference
Sun Cirtification
srting doubt on ==