How memory is stored in the heap? As in this video only about stack its explained.
Or is it because heap can only store non-primitive data type. will it function same like stack? i.e one after the other?
The functioning of heap
@kalindiyadav5 hi kalindi no that is not the case, actually heap is used for dynamic memory allocation for java objects.
Where reference for object is stored in stack and object is allocated memory in heap in any random order.
Say Student ram = new Student();
So ram is a reference stored in stack and new Student() is a object stored in heap. So basically this is how heap memory works.
If your query is cleared do mark the doubt resolved and rate full else feel free to ask your query!
Happy coding!
I studied somewhere that heap allocates memory for non primitive data types
@kalindiyadav5 So here Student class is non primitive. I just told you the working here.
Even your array is non primitive. I am not denying the fact here, by dynamic memory allocation I am conveying the same! The reference is stored in stack and object is stored in heap.
If your query is clear then resolve it or feel free to ask!