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?
java
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?
Best Answer
There is no difference between the two "in memory."
From the Java Language Specification, 4.12.5. Initial Values of Variables :
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.