+ 2

Why are JAVA objects stored in Heap

JAVA objects and their instances are stored in Heap but Heap stores Dynamic Memory. I do get the concept about the Primitive Variables, in java, stored in Stack.

7th May 2017, 11:06 AM
Shahzaib Waseem
Shahzaib Waseem - avatar
1 Answer
+ 6
Primitives don't need much memory so it's ok to store in the stack. And some of them use the constant pool, what saves memory. Objects stored the heap because they handled dynamic, the memory is allocated in the heap in run time. If an object not used anymore the garbage collector delete it to free memory. A good article: http://www.journaldev.com/4098/java-heap-space-vs-stack-memory
7th May 2017, 11:17 AM
Szabó Gábor
Szabó Gábor - avatar