Answer the question
In order to leave comments, you need to log in
Why is there no stack memory in Java?
A question from a C++ nickname learning Java.
Why in Java all objects are created only in heap and there is no stack memory? Is it just Java's philosophy of reducing complexity for the developer at the cost of making decisions less efficient from a machine perspective? What problems, besides stack overflows, are solved by abandoning the faster stack memory type? Or was the overflow problem enough to make that decision?
Answer the question
In order to leave comments, you need to log in
Apparently, your question should be understood as follows:
Why can't objects in Java be created on the stack?
In my opinion, the main reason for this step is the abolition of pointers from the language and a uniform scheme for working with memory (gc).
PS By the way, if there really was no stack, then you could not perform nested method calls.
It's not clear what you asked. What other stack are we talking about? In java, there is a stack at the bytecode level and is actively used. And if you rise to the level of the language, then here in c ++ everything is exactly the same, sort of. But then it’s all the more incomprehensible, and here is the “complexity for the developer”, if with this t.z. the stack is completely transparent. Explain in what aspect do you mean the differences? In java, strictly speaking, it cannot be said that objects are “created only in heap”, everything is somewhat more complicated (including gc), including here on Habré there were notes about the memory model in java.
In my opinion, all primitive types and their wrappers, at least local within methods, in Java are allocated on the stack of the current thread.
But for sure, the JVM actively uses the stack internally to allocate objects.
The main point here is the stack, it's a local thing for a particular thread, so in complex applications it hardly makes sense to allocate a lot of memory on the stack from one thread.
Moreover, the default stack size for one thread in Java is small, 512kb by default.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question