Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Primitive data type on Stack or Heap?

Tags:

java

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 ?

like image 926
dev_marshell08 Avatar asked Apr 01 '14 07:04

dev_marshell08


People also ask

Is Java primitive data type stored on stack or heap?

There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory.

Are Java primitives stored on the stack?

➲ In Java, all data type for primitive type variables is stored on the stack. ➲ For reference data types, the stack holds a pointer to the object on the heap.

Is primitive datatypes are allocated on a stack?

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.

Are primitive values stored on the stack?

The eight primitives defined in Java are int, byte, short, long, float, double, boolean and char. These aren't considered objects and represent raw values. They're stored directly on the stack (check out this article for more information about memory management in Java).


1 Answers

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.

like image 129
Dawood ibn Kareem Avatar answered Oct 08 '22 03:10

Dawood ibn Kareem