type name;
For reference types,this notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable,this declaration also reserves the proper amount of memory for the variable.
Above are the exact lines i read from oracle docs java tutorials.
My question is, when does memory allocation take place. During compile time or run-time??
Is that same for both primitive and reference variables?
Runtime.
As Java is executed by a
VM, it does not make sense allocating memory at compile time.‘Local variables‘ like 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 concerning memory management (allocation, deallocation, garbage-collection) is
JVMimplementation dependent and happens only at runtime (except for primitive and String constants of course).