Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Java primitives go on the Stack or the Heap?

I just know that the non-primitives (the objects) go on the heap, and methods go on the stack, but what about the primitive variables?

--update

Based on the answers, I could say the heap can have a new stack and heap for a given object? Given that the object will have primitive and reference variables..?

like image 267
The Student Avatar asked Sep 05 '10 15:09

The Student


People also ask

Are primitives stored in 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 primitives stored on the heap in Java?

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.)

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.

Does Java use heap or stack?

Java Heap Space is used throughout the application, but Stack is only used for the method — or methods — currently running. The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application.


2 Answers

Primitives defined locally would be on the stack. However if a primitive were defined as part of an instance of an object, that primitive would be on the heap.

public class Test {     private static class HeapClass {         public int y; // When an instance of HeapClass is allocated, this will be on the heap.     }     public static void main(String[] args) {         int x=1; // This is on the stack.     } } 

With regards to the update:

Objects do not have their own stack. In my example, int y would actually be part of each instance of HeapClass. Whenever an instance of HeapClass is allocated (e.g. new Test.HeapClass()), all member variables of HeapClass are added to the heap. Thus, since instances of HeapClass are being allocated on the heap, int y would be on the heap as part of an instance of HeapClass.

However, all primitive variables declared in the body of any method would be on the stack.

As you can see in the above example, int x is on the stack because it is declared in a method body--not as a member of a class.

like image 94
Jack Edmonds Avatar answered Sep 29 '22 23:09

Jack Edmonds


All local variables (including method arguments) go on the stack; objects and all their fields are stored in the heap. Variables are always primitives or references to objects.

Java implementations may actually store objects on the heap in such a way that it still complies with the specification. Similarly local variables may be stored in registers or become indistinct through optimisation.

like image 40
Tom Hawtin - tackline Avatar answered Sep 29 '22 21:09

Tom Hawtin - tackline