39

If i am correct Primitive data type defined locally would be on the stack. However if a primitive data type were defined as part of an instance of on object that primitive would go on heap.

class  Test
{
    int y=10; // defined as part of the class

    public void function1(){
        int x = 5; // defined locally
    }

    public static void main(String[] args) 
    {
        Test obj = new Test();
    }
}

So in the above code will x be stored on the stack and y on the heap ? I am confused how they are stored and why does it matter stack or heap ?

dev_marshell08
  • 1,051
  • 3
  • 18
  • 40

5 Answers5

81

When a method is called, certain data is placed on the stack. When the method finishes, data is removed from the stack. At other points in a program's execution, data is added to the stack, or removed from it.

Therefore, if you have a variable which is intended to outlive the execution of the method that created it, it needs to be on the heap. This applies both to any objects that you create, and any primitives that are stored within those objects.

However, if a variable is intended to go out of scope shortly after its creation - say, at the end of the method in which it's created, or even earlier, then it's appropriate for that variable to be created on the stack. Local variables and method arguments fit this criterion; if they are primitives, the actual value will be on the stack, and if they are objects, a reference to the object (but not the object itself) will be on the stack.

In your specific example, x is unusable as soon as function1 finishes running. So it's reasonable for it to be created on the stack. At the end of function1, data is effectively removed from the stack, including x. On the other hand, the variable y is expected to still exist for as long as the containing object exists; if it were created on the stack, it would cease to exist once the object constructor which created it finishes running. Therefore y must be created on the heap.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
35

Only local primitive variables and references to object (i.e. variable declared in method) are stored in stack. Others are stored in heap

Prakash Bhagat
  • 1,406
  • 16
  • 30
6

I am pretty sure you got your answer by now... one thing to notice. Even if you variables will be stored on the heap, Java will still load their values on the stack when it needs to act upon them. That is the way JVM works - it is a stack language.

Eugene
  • 117,005
  • 15
  • 201
  • 306
3

Instance level variables(can also be primitives) are part of instances (objects). So, they will be allocated on the heap (as part of the object.)

Method level / Local variables : Stack (for Primitives) or Heap(Objects with references on the Stack). Strings can be allocated either on Stack or on heap.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 2
    All good except for the final sentence. Strings are just like any other object - they can ONLY be allocated on the heap. References to strings can, of course, exist on the stack. – Dawood ibn Kareem Oct 16 '18 at 18:45
  • 2
    to be specific, Strings are stored part of StringPool within the heap. – Durja May 03 '20 at 03:09
0

When you initiate a Test instance, all of it's fields such as y will be stored in heap.

When you call function1, the local varaible x will be push into the stack.

But in your example, x is not stored in stack because function1 is never called.

Weibo Li
  • 3,565
  • 3
  • 24
  • 36