This might sound stupid but I am still not clear about the Java Stack and the memory heap. What I know from studying is following:
1) All the method calls goes on stack.
2) All the memory allocated locally goes on memory heap (not very clear about this point)
3) All the memory allocated by new operator (either in a method or in a class) goes on memory heap.
I am worried about the below cases:
1) If I create a int variable in a method and return it, where does it go ( I believe it goes on stack, but need clarification).
2) If I create a new object in a method it goes on heap memory as it exists even after the methods execution is over(I understand this happens because the hash code of the object created by java remains same when I assign this object to some external reference variable or I return this object ).
3) My problem is what happens if I am not assigning the object mentioned in point 2 to any reference or I am not returning this. Is it still created on heap? Logically it should be but please enlighten me.