Memory allocation for primitive and reference variables

type name;

For reference types, this tells the compiler that you will use the name to refer to data whose type is a type. With a primitive variable, this declaration also reserves the required amount of memory for the variable.

Above are the exact lines I read from the oracle docs java tutorials.

My question is when is the memory allocation. At compile time or runtime? Is it the same for primitive and reference variables?

0
source share
2 answers

Runtime

Since Java runs VM , it is impractical to allocate memory in compile time.

"Local variables", such as function arguments or variables inside a function, are only "allocated" on the stack (primitive value or reference). Objects are always created on the heap .

But: everything related to memory management (allocation, deallocation, garbage collection) depends on the JVM implementation and occurs only at runtime (with the exception of primitive and string constants of course).

+4
source

The allocation of memory occurs at runtime, and allocation for storage is required for both reference types and primitives. To be more precise, I cannot understand what it means memory allocation in compile time (for java ).

+2
source

All Articles