This may sound silly, but I still don't understand what Java Stack and heap of memory are. What I know from learning is the following:
1) All method calls go to the stack.
2) All allocated memory locally goes into a heap of memory (not very clear about this point)
3) All memory allocated by the new operator (either in the method or in the class) goes to the heap of memory.
The following cases bother me:
1) If I create an int variable in the method and return it where it goes (I believe that it goes on the stack, but needs clarification).
2) If I create a new object in the method, it goes into heap memory, because it exists even after the methods have completed execution (I understand this is because the hash code of the object created by java remains the same when I assign this object to some external reference variable or I return this object).
3) My problem is what will happen if I do not assign the object mentioned in clause 2 to some reference or I do not return it. Is it still created on the heap? Logically, this should be, but please enlighten me.
source share