Java Reference
In-Depth Information
Figure 3-2. Memory states using reference variables assignments
You can also assign the reference of an object stored in one reference variable to another reference variable.
In such cases, both reference variables refer to the same object in memory. This can be achieved as follows:
// Declares String reference variable str1 and str2
String str1;
String str2;
// Assigns the reference of a String object "Hello" to str1
str1 = new String("Hello");
// Assigns the reference stored in str1 to str2
str2 = str1;
There is a reference constant (also known as reference literal) null , which can be assigned to any reference
variable. If null is assigned to a reference variable, it means that the reference variable is not referring to any object
in memory. The null reference literal can be assigned to str2 .
str2 = null;
The memory states after execution of all of the above statements are depicted in Figure 3-3 .
Figure 3-3. Memory states using null in the reference variables assignments
 
Search WWH ::




Custom Search