Junior — Middle
Where in memory are primary type objects and variables placed?
sobes.tech AI
Answer from AI
In Java, variables of primitive types (int, boolean, double, etc.) are usually placed on the stack if they are local variables of a method. If primitives are fields of an object, they are stored in the heap along with the object itself.
Objects are always placed in the heap. Reference variables to objects are stored on the stack if they are local variables, or in the heap if they are fields of other objects.
Thus:
- Local primitives — stack
- Primitive fields in objects — heap
- Objects — heap
- References to objects — stack (local) or heap (object fields)