Let's start with the following statement:
String x = null;
1. What exactly did this statement do?
Recall what a variable is and what is a value. A common metaphor is that a variable is similar to a box, and then you can use the box to put something in it, and you can store a value with a variable. When declaring a variable, we need to specify its type.
There are two main types in Java: Basic types and reference types. The base type stores the value, and the reference type stores the reference. In this example, the initialization statement declares a variable "x", and "X" stores the string reference, which is null here.
The following is a visual explanation of the concept:
If x= "ABC", it looks like this:
2The true face of NULL in memoryWhat exactly is null in memory, or what is the value of NULL in Java?First, NULL is not a legitimate object instance, so there is no memory allocated for it. It is simply a value that detects whether a corresponding reference points to an object.From the JVM specification:
The Java Virtual Machine specification does not specify a specific value for null.3.x true in memoryNow we all know what null is. We all know that a variable is a storage location and is an associated symbolic name that contains some values. Where does x reside in memory?
From the Java Virtual Runtime zone above, we know that each method has a private stack frame, and the local variable is stored in that frame.
The true face of NULL in Java