Sub topic 4
References, shared objects, null, and identity
When you create an object with new, Java gives you a reference to it. A reference is a value that lets your code find and use an object. A reference variable holds that reference, not the object itself.
This matters because two variables can refer to the same object. A change made through one variable is then visible through the other.
Copying a reference
Look at these two lines:
Counter first = new Counter();
Counter second = first;The first line creates a Counter object and stores a reference to it in first. The second line copies that reference into second. It does not create another object.
If you call a method through second that changes the object, you can see that change through first too.
Try it: shared and separate objects
Save this program as ReferenceDemo.java:
class Counter {
int value = 0;
void increment() {
value++;
}
}
public class ReferenceDemo {
public static void main(String[] args) {
Counter first = new Counter();
Counter second = first;
second.increment();
System.out.println("first value: " + first.value);
System.out.println("second value: " + second.value);
System.out.println("Same object: " + (first == second));
Counter separate = new Counter();
separate.increment();
System.out.println("separate value: " + separate.value);
System.out.println("Same as separate: " + (first == separate));
first = null;
if (first == null) {
System.out.println("first refers to no object");
}
System.out.println("second still has: " + second.value);
}
}Compile and run it with:
javac ReferenceDemo.java
java ReferenceDemoThe output is:
first value: 1
second value: 1
Same object: true
separate value: 1
Same as separate: false
first refers to no object
second still has: 1The call second.increment() changes the shared object. Both first.value and second.value therefore show 1.
The variable separate refers to a different object created by another call to new. Both objects have a value of 1, but they are still two objects.
Object identity and `==`
Object identity means which particular object a reference points to. For object references, == checks whether both references point to the same object.
In the example, first == second is true because the variables refer to one object. first == separate is false because they refer to different objects, even though both counters hold the same value.
Two null references also compare as equal with ==. This does not mean they refer to an object; neither one does.
What `null` means
null is a special value that means a reference does not point to an object. In the example, this line changes the reference stored in first:
first = null;It does not change or erase the counter. The variable second still refers to that object and can read its value.
Trying to use a field or call a method through null causes a NullPointerException. This is a Java error that stops the normal flow of the program unless it is handled. For example, calling first.increment() after first = null would cause that error.
Check a reference before using it when it might be null. The example uses first == null for that check. You can also use first != null to check that it refers to an object.
Exercise
If a and b refer to the same counter, what happens to b when you assign null to a?
Show answer
b still refers to the counter. Only the reference stored in a changes.
Exercise
Two different Counter objects both have a value of 1. What does == report when you compare references to them?
Show answer
It reports false because the references point to different objects.