Do Java primitives go on the Stack or the Heap?
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.
Java Primitive data type on Stack or Heap?
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.
Java primitive object instanciation: heap or not?
First of all:
int x2 = new Integer(100);
This means an Integer
object is created, outboxed (the JVM returns its intValue
) and 100
assigned to an int
primitive. The Integer
object is no longer referenced and can be disposed (of course maybe the JIT can optimize that to int x2 = 100
).
I assume you are talking about local variables, because attributes are part of the object and so lie with it in the heap.
int x1 = 100;
An int variable is declared in the stack and assigned a value
String s1 = "Hello";
An String object is created (or referenced, see String.intern()
) and a pointer is added to the stack.
The other possibilities are exactly the same.
Where instance variables(primitives) are stored in java? Is anyhow stack related to instance variables storage?
Primitive variables are stored in the same places all variables are stored (including references):
- Within objects created (allocated) on the heap, or
- Within method stack frames as local variables, or
- Within static areas of their containing class (which are on the heap).
Related Topics
Java: Object to Byte[] and Byte[] to Object Converter (For Tokyo Cabinet)
How to Parse a JSON String to an Array Using Jackson
Java - Delete Line from Text File by Overwriting While Reading It
Is This a Bug in Files.Lines(), or am I Misunderstanding Something About Parallel Streams
Use of Java [Interfaces/Abstract Classes]
How to Compile a Java Source File Which Is Encoded as "Utf-8"
How to Clear or Empty a Stringbuilder
Hibernate: Flush() and Commit()
How to Get Current Working Directory in Java
Adding Points to Xyseries Dynamically with Jfreechart
What Is the "Default" Implementation of Method Defined in an Interface
Should You Always Code to Interfaces in Java
Where Do I Have to Place the Jdbc Driver for Tomcat's Connection Pool
Documentlistener Java, How to Prevent Empty String in Jtextbox