+ 3
Can Someone simply explain what is stack and heap and their differences pls.
What is stack and heap? What are their differences?
7 ответов
+ 12
Steve Sajeev you can even give the resource like rather then copying and pasting an answer..
https://www.guru99.com/stack-vs-heap.html#:~:text=Stack%20is%20a%20linear%20data,you%20to%20access%20variables%20globally.
Anyways
---
K.A.D Helesh Madhuka Athukorala
Stack and heap memory are two terms programmers start hearing once they started programming but without any clear and definite explanation.
Lack of knowledge of what is a heap in Java and what is stack memory in Java results in misconceptions related to stack and heap!
To add to this confusion, a stack is also a data structure that is used to store elements in LIFO(Last In First Out) order and available in Java API as java.util.Stack.
In general, both stack and heap are part of memory, a program is allocated and used for different purposes. Java program runs on JVM which is launched as a process by "java" command.....
Java also uses both stack and heap memory for different needs!
+ 3
Stack is a linear data structure whereas Heap is a hierarchical data structure.
Stack memory will never become fragmented whereas Heap memory can become fragmented as blocks of memory are first allocated and then freed.
Stack accesses local variables only while Heap allows you to access variables globally.
Stack variables can’t be resized whereas Heap variables can be resized.
Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order.
Stack doesn’t require to de-allocate variables whereas in Heap de-allocation is needed.
Stack allocation and deallocation are done by compiler instructions whereas Heap allocation and deallocation is done by the programmer.
these are not my words. Copied from:
https://www.guru99.com/stack-vs-heap.html#:~:text=Stack%20is%20a%20linear%20data,you%20to%20access%20variables%20globally.
for information read the above thread.... 😊😊
+ 3
Mm.. sorry.. done....
+ 3
The heap is used for dynamic memory allocation and the stack for static memory.
+ 2
Steve Sajeev thank you for your link .
+ 2
Piyush[21 Dec❤️] thank you for the link.
+ 1
Thank you guys.your answers are really helpful for a person like me.