Can Someone simply explain what is stack and heap and their differences pls. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can Someone simply explain what is stack and heap and their differences pls.

What is stack and heap? What are their differences?

4th Dec 2020, 3:17 PM
KADHM
KADHM - avatar
7 Answers
+ 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!
4th Dec 2020, 4:20 PM
Piyush
Piyush - avatar
+ 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.... 😊😊
4th Dec 2020, 3:36 PM
Steve Sajeev
Steve Sajeev - avatar
+ 3
Mm.. sorry.. done....
4th Dec 2020, 4:31 PM
Steve Sajeev
Steve Sajeev - avatar
+ 3
The heap is used for dynamic memory allocation and the stack for static memory.
4th Dec 2020, 7:55 PM
Sonic
Sonic - avatar
+ 2
Steve Sajeev thank you for your link .
6th Dec 2020, 1:45 AM
KADHM
KADHM - avatar
+ 2
Piyush[21 Dec❤️] thank you for the link.
6th Dec 2020, 1:46 AM
KADHM
KADHM - avatar
+ 1
Thank you guys.your answers are really helpful for a person like me.
6th Dec 2020, 1:42 AM
KADHM
KADHM - avatar