Can an object be stored on the heap?

This is just for my understanding. Objects that are members of the class fall into the heap, while local variables and value types get o stack. Is there a possibility and the ability to force an object to be pressed on Stack or heap?

+3
source share
4 answers

Objects that are members of the class are in a bunch.

No. Objects fall into a heap.

... while local variables and value types [go] the stack.

No. All local variables are allocated on the stack. If they are references to objects, the corresponding objects are on the heap if they exist.

Is it possible and possible to push an object on a stack or a bunch forcibly?

. . .

+2

, Java . , . ++, , . , , .

+5

- , .

, , , , "" , , . , , .

, , , , , ( - jni- ).

+2

In Java, everything highlighted by the new is allocated from the heap. Local variables of primitive types are allocated from the stack.

+2
source

Source: https://habr.com/ru/post/1733850/


All Articles