So every Java program runs in a Virtual Machine environment, but how is a Virtual Machine made up? Essentially, there are two areas of memory which are used by Java programs. These are commonly known as the stack and the heap.
The stack is the memory set aside as scratch space for a thread of execution. When a method is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When the method returns, the block becomes unused and can be recycled the next time a function is called. The stack is always reserved in a LIFO (Last In First Out) order, which means that the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack meaning that freeing a block from the stack is nothing more than adjusting one pointer.
The heap is the area of memory set aside for dynamic allocation. Unlike the stack, you can allocate a block at any time in your code and free it as well at any time...