0

while studying a linked list implementation i need a clarify how the reference and object store in stack and heap for this kind of scenario where object it self has references,

public class MyLinkedList {

    private Node head;
    private int listCount;

    public MyLinkedList() {
        head = new Node("0");
        listCount = 0;
    }

    public void add(Object data) {
        Node nodeTemp = new Node(data);
        Node nodeCurr = head;

        while (nodeCurr.getNext() != null) {
            nodeCurr = nodeCurr.getNext();
        }

        nodeCurr.setNext(nodeTemp);
        listCount++;

    }
}

public class LinkedListMain {
    public static void main(String[] args) {
        MyLinkedList ls = new MyLinkedList();
        ls.add("1");
}

Now MyLinkedList object is refer by "ls" reference which is in stack and the MyLinkedList it self is in heap. That i understood.

But then from the MyLinkedList constructor when we create new Node which refer by "head" reference where does that "head" reference store? My doubt is since "Node head" is inside (belong to) MyLinkedList object, does "head" store in stack with "ls" or is it kind of inside MyLinkedList object?

1
  • head is the reference of Node. Is Node is user defined class? Commented Apr 28, 2015 at 5:19

3 Answers 3

2

Two important things about Java you need to understand:

  1. All Java objects are allocated in the Java heap. All of them.
  2. In Java, variables are never objects. Never. But a variable can be a reference to an object. (Or a variable can be a primitive like int, but those are also not objects.)

What this means is that your main method allocates a MyLinkedList object, on the Java heap, and stores a reference to that object in a variable named ls. That MyLinkedList object (which itself doesn't have a name) can store a reference to a Node object (which will also be stored on the Java heap) in a local field called head.

No other object is ever stored "inside" any object. Only references to other objects are stored inside.

Caveat: While this answer is correct I'm regard to how the Java language works, the runtime is allowed to make various optimizations as long as you can't tell the difference. For example, "the Java heap" is not a heap in the algorithmic sense, and often not even in the same sense as "the C++ heap". The Java JIT is allowed to allocate Java objects from a stack-like structure (the younggen) or even from the current stack (due to escape analysis). That said, those implementation details are not relevant when you're just learning the language.

Sign up to request clarification or add additional context in comments.

Comments

0

The "MyLinkedList" object allocated in main() is reference by "ls". "ls" is on the stack - a local variable of main. The members of this instance of "MyListedList" are on the heap. In add(), nodeTemp and nodeCurr are stack variables which are references to nodes. The nodes themselves are on the heap. "head" is a class member variable, and is on the heap, along with listcount.

In Java, (non-static) class members must always be on the heap.

6 Comments

what about both primitive and reference static class members please?
static class members (primitives) are not on the stack. They are allocated when the program starts. I don't know specifically if they are on the heap or part of the program image (as they are in C) - but it doesn't really matter. It is important to understand the difference between a reference to an object and the object itself. A reference can have the value "null", or be a reference to an object. Objects are always accessed via a reference - therefore "static Object foo" - the storage for the reference itself ("foo") is static, but whatever object it references will always be in the heap.
Also, the amount of memory needed to store a reference is about the same as the size of an integer. Depending on your machine, it will typically be either 4 or 8 bytes. The size of an object depends on its non-static members (plus some small amount of overhead). So an object with 500 int members will require about 4*500=2000 bytes of memory.
Thanks. Just for better understanding i am asking, So as you said in answer "In Java, (non static) class members must always be on the heap." And static class members (primitives) allocated when the program starts(heap or program image). I hope by mean program start you refer as class load know?So does static class members (object reference) also allocated as same as when the program starts and store in heap or program image? and not on stack? I understand that object reference by "static Object foo" is always in heap. Just need to know where the static class reference "foo" store?
"foo" would be stored in the same place as primitive class members. I don't know for sure with Java, but I suspect that these members are allocated on the heap at class load time. (See also static initialization blocks). If you have "static Object foo = new Something()", the allocation of the space for "foo" (and the rest of the statics) will happen before Something() is allocated. Experiment: Make the Something() constructor examine (and print) all the static members of MyLinkedList. Add "foo" and "foo2" as the first, and last static members in MyLinkedList - then run and observe the ordering
|
0

All the instance variables & objects are stored on the heap. Whereas local variables are on the stack. headwill be on the heap & ls will be on stack.

public class MyLinkedList {

    private Node head;
    private int listCount;

head & listCount both will be on heap.

2 Comments

So by mean instance variables you mean the class members right? So head & listCount both will be on heap and local variable for method add, nodeTemp and nodeCurr in stack
yes, Inside the Java virtual machine, each thread is awarded a Java stack, which contains data no other thread can access, including the local variables, parameters, and return values of each method the thread has invoked. The data on the stack is limited to primitive types and object references. In the JVM, it is not possible to place the image of an actual object on the stack. All objects reside on the heap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.