What is the difference in memory between a variable assigned to null and one not assigned?
I know that there is a difference in usage, but what is the difference in memory?
1
There is no difference between the two “in memory.”
From the Java Language Specification, 4.12.5. Initial Values of Variables :
Every variable in a program must have a value before its value is
used:
- Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):
…
- For all reference types (§4.3), the default value is null.
…
- A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16 (Definite Assignment)).
Reference variables at the class or object level will be assigned a null value.
Reference variables at the method (stack) level will have an unspecified value (using the C++ term). In practice this is often null, but the standard does not specify what is in the reference variable, only that it must be assigned before use. Using a reference variable on the stack in any way other than assignment as its first use will result in a compile error.
4
What is the difference between a variable assigned to null and others not assigned in memory?
There is no such thing as an unassigned variable in Java. Class data members that don’t have an explicit assignment are assigned the default value for the type, null for an object, or something akin to 0 for primitive types.
Local variables live on the stack. (Non-primitive locals such as MyType foo
also live on the stack, as references.) The stack allocation is simple. If a block contains declarations for local variables, the space needed by those local variables is reserved on the stack. Whether values are assigned to those local variables on adjusting the stack to accommodate those local variables is irrelevant.
The reason it doesn’t matter is whether local variables are or are not assigned a value at the time execution enters the block is twofold. One reason is that the size needed doesn’t change. Primitives have a fixed size, and non-primitives are actually references. (The referenced object lives on the heap rather than the stack.) The second reason is that using a local variable that hasn’t been assigned a value is a compilation error. This leaves it up to the Java runtime whether to give some value (not necessarily the default) to a declared but unassigned local variable.
4
First, there is really no need to worry about the space used by local variables. In one line I can allocate an array of 100,000 integers which will take more space than all the local variables that you are ever going to use.
Second, your code is compiled by a compiler which is likely to be clever. If your variable isn’t initialized, it isn’t used (because the compiler doesn’t allow to use uninitialized variables). But if you initialize it to nil, it still isn’t used! The compiler will easily figure out that it isn’t used and not actually use space for the variable at all. Even if it is used, the compiler will easily figure out that the value is nil, and use nil instead of the the variable.
But third, this is all micro-optimisations that are not going to help you optimise a program in any way. You are trying to save four or eight bytes. Concentrate on saving megabytes if you want to save anything.
And last, while some people claim variables should always be initialised, that’s not true in Java. You should only ever set a variable to a meaningful value. Let’s say you want to set a variable to the name of a person, and you forget to do this in one code path. If you blindly initialize the variable to nil, the compiler can’t tell you. Without the initialisation, the compiler can and will tell you about your mistake.