Well, you need to use a bunch if you want to use objects. Objects are essentially pointers to the stack (or inside other objects) to pieces of memory on the heap.
Unlike C ++, where you can put objects on a stack or a heap, Java does things differently.
Even in C ++, it is recommended to use a bunch for objects that must survive the function in which they were created. You can probably avoid this, but you may run into a performance issue with all copy constructors.
As for your editing:
Is there any other kind of memory that we can think of an alternative to heap and stack?
Of course, there are: static data elements, those where there is only one for each class, and not one instance of the object should go somewhere. They cannot be on the stack, as they can disappear when the function exits. And they cannot belong to a specific object.
These (at least in the Sun / Oracle JVM) fall into the Method area.
In addition, you should not think about having one stack. Each thread gets its own stack when created.
There is also a pool of runtime constants and stacks for native (non-Java) calls.
There is a lot of information about the internal components of the JVM here regarding this aspect, but keep in mind that there may be a difference between the logical machine and the implementation.
paxdiablo
source share