Sub topic 12
Reference Equality versus Value Equality
In Java, == and equals can answer different questions about objects. An object is a piece of data created by a program. A reference is a value that points to an object.
For references, == asks: “Do these references point to the same object?” This is called reference equality.
Value equality asks whether two objects have equal contents, according to their class. A class describes a kind of object. For example, the String class represents text, and its equals method compares the text. A method is an action you can call in Java.
Compare two strings
Save this program as ReferenceEquality.java:
import java.util.Objects;
public class ReferenceEquality {
public static void main(String[] args) {
String first = new String("Java");
String second = new String("Java");
String alias = first;
String missing = null;
System.out.println(first == second);
System.out.println(first.equals(second));
System.out.println(first == alias);
System.out.println(missing == null);
System.out.println(Objects.equals(missing, first));
System.out.println(Objects.equals(missing, null));
}
}Run it with:
javac ReferenceEquality.java
java ReferenceEqualityThe output is:
false
true
true
true
false
trueHere is what happens:
new String("Java")creates a new string object each time. Sofirstandsecondpoint to different objects. Their references are not equal, but their text is equal.alias = firstcopies the reference. Both variables now point to the same object, sofirst == aliasistrue.nullmeans a reference points to no object. The last three lines check for or comparenull, as explained below.
Compare safely with null
You can use == null to check whether a reference points to no object. But calling missing.equals(first) would cause a NullPointerException: an error caused by trying to use null as an object.
The example imports Objects from Java’s standard library. Its Objects.equals method handles null safely:
| Call | Result | Why |
|---|---|---|
| `Objects.equals(missing, first)` | `false` | One value is `null`; the other is not. |
| `Objects.equals(missing, null)` | `true` | Both values are `null`. |
| `Objects.equals(first, second)` | `true` | Both are strings with the same text. |
Equality depends on the class
For strings, equals compares text, including its letter case: "Java" and "java" are not equal.
Other classes can define value equality differently. Some classes do not define their own value comparison, so their equals method uses reference equality instead. Check the class’s documentation when you need to know what equality means for its objects.
For primitive values, such as int, == compares the values themselves. The reference rule applies when == compares object references.
Exercise
Exercise
Given String a = new String("cat"); String b = a; String c = new String("cat");, what are the results of a == b, a == c, and a.equals(c)?
Show answer
They are true, false, and true. a and b point to the same object; c points to a different object with the same text.