I've looked through the different threads, and there are a lot of conflicting information out there. The most useful article I found was this: Java theory and practice: Urban performance legends, revisited but it's from 2005.
So I was wondering what is the state of the Java standard regarding heap allocation. In the release notes for Java 7 it says:
(The server compiler) does not replace a heap allocation with a stack allocation for non-globally escaping objects.
I got the impression from the other article that this is indeed possible to do in the compiler, but it seems to not be required by the standard.
A related question which doesn't seem to answer my question is Stack and Heap memory in Java The accepted answer for this question says that only primitives may be put on the stack. There does not seem to be any consensus on that. Secondly, I'm obviously asking a different question.
I understand that in Java everything is a reference. That said, would a compiler allocate everything on the heap even if it is only reachable from a stack reference?