In case of array of objects where exactly memory is allocated. What we say is array reference will be created onto stack, that's fine. This array reference in turn points to object refernces. In which area of memory they are allocated?(Either heap or stack)
Thanks for your answer to my question. But I wanted it more clearly. I would be really very thankful if u plz help me out. When we create an array ref. it is allocated on stack because it's a ref. That ref. in turn points to object referenes(If it is array of objects). Here exactly I want to know where actually this array of refs would be created. Then in turn these refs would refer to the objects which would be created on heap.
You are right with Array reference living in the stack (e.g. mooseArray). Now if that Array is an array of objects, i.e. a number of reference types in the array object, they would all belong in the heap.
Just as how instance variables reside in the heap with an object, despite it being a primitive type or reference type, the array elements also reside in the heap regardless of it being a primitive or reference type.
mooseArray mooseArray mooseArray >> All in the heap mooseArray mooseArray
This is what i think. Do correct me if am wrong.
author and iconoclast
Originally posted by Rajashekar Subramany: This is what i think. Do correct me if am wrong.
You're correct in your thinking, although your example has problems.
I think what confuses people is the oft-repeated, but frankly wrong, statement that "references are on the stack, and objects are on the heap." What goes on the stack are local variables. Any variable that you declare inside a method is on the stack. This is true for all kinds of variables, primitive and refererence. It has nothing to do with the type of the variable -- only where it's defined.
Instance variables -- non-static variables declared outside of any method -- are part of an object. They are the object, in fact. When we say that "an object is allocated on the heap", what we're saying is that memory to hold the instance variables of that object is allocated in the heap area. Again, the data type of the variables is irrelevant: primitive members and reference members alike are stored in the object's same little block of memory on the heap.
Anything created by "new" is an object; this include arrays. Arrays are instances of special classes that extend java.lang.Object, just like any other class. These objects, just like all other objects, are allocated on the heap, and that allocated memory contains all their data. The array elements are like the "member variables" of the array, and as such, they are part of the object living on the heap. I should say here, to be precise, that nowhere in the JLS or VM spec does it say anything about how objects are laid out in heap memory. You have to realize that much of what we're saying here is conceptual rather than a description of some physical reality in the computer.
Anyway, based on the "mooseArray" example, we can't tell where mooseArray is, because if we only see that one line of code, we don't know if it's in the body of a method or at class level, and it's really this kind of one-line example that gives rise to the confusion we're trying to remedy.
The short answer: arrays, including the element data they contain, are objects allocated on the Java heap. [ January 21, 2006: Message edited by: Ernest Friedman-Hill ]