I hope the preceding example and explanation gave you a good idea about how JVM uses heap and stack. So, let’s just summarize these two points and note down a few key differences as follows:
- Stack is faster than heap memory.
- Stack provides a storage for local variables, including primitive and object variables referencing to objects in the heap memory.
- Stack is LIFO, whereas heap is not.
- Stack memory is smaller than heap memory.
- Stack is specific to one thread of execution, whereas Heap memory is available to all parts of the program.
- As mentioned earlier, stack is specific to one thread of execution so only the current executing thread can access stack memory whereas heap memory is accessible globally.
- Stack ...