Sub topic 12
Comparing object identity and logical equality
Two objects can contain the same information without being the same object. Java offers different ways to compare them, depending on what you want to know.
An object is a piece of data created from a class. A reference is a value that points to an object. Two references can point to the same object, or they can point to separate objects.
Identity and `==`
Object identity means being the exact same object. For references, the == operator checks whether both sides point to the same object.
For example, if you copy a reference into another variable, both variables point to the same object. Comparing them with == gives true.
The value null means that a reference does not point to an object. If both references are null, == also gives true.
Logical equality and `equals`
Logical equality means that two objects count as equal based on their contents or some other rule. They do not have to be the same object.
The equals method compares objects according to the rule supplied by their class. A class is a definition used to create objects. For example, the String class represents text, and its equals method checks whether two strings contain the same characters in the same order.
Not every class compares contents this way. If a class does not provide its own equals rule, it uses the default rule from Java’s Object class. That rule checks identity.
Worked example
Save this program as EqualityDemo.java. The Ticket class has a field, or stored value, named code. It does not provide its own equals rule.
import java.util.Objects;
class Ticket {
String code;
Ticket(String code) {
this.code = code;
}
}
public class EqualityDemo {
public static void main(String[] args) {
String first = new String("A12");
String second = new String("A12");
String shared = first;
String missing = null;
System.out.println("first == second: " + (first == second));
System.out.println("first.equals(second): " + first.equals(second));
System.out.println("first == shared: " + (first == shared));
System.out.println("missing == null: " + (missing == null));
Ticket ticket1 = new Ticket("A12");
Ticket ticket2 = new Ticket("A12");
System.out.println("ticket1.equals(ticket2): "
+ ticket1.equals(ticket2));
System.out.println("same ticket codes: "
+ Objects.equals(ticket1.code, ticket2.code));
System.out.println("missing equals null: "
+ Objects.equals(missing, null));
}
}Run it with these commands:
javac EqualityDemo.java
java EqualityDemoThe output is:
first == second: false
first.equals(second): true
first == shared: true
missing == null: true
ticket1.equals(ticket2): false
same ticket codes: true
missing equals null: trueHere is why:
firstandsecondpoint to separate strings, so==givesfalse. Their text matches, soequalsgivestrue.sharedholds a copy of the reference infirst. Both point to the same string.missingisnull, so it does not point to an object.- The two tickets have matching codes, but they are separate objects. Since
Ticketuses the defaultequalsrule,ticket1.equals(ticket2)isfalse. - The last ticket comparison checks the codes instead. Those strings have matching text.
The example uses new String(...) to make separate string objects clear. Java can share strings written as text in code, so comparing such strings with == can give a result you did not expect. Use equals when you want to compare their text.
Comparing safely with `null`
You cannot call a method on null. For example, missing.equals(first) would cause a NullPointerException, an error caused by trying to use a missing object.
Objects.equals(a, b) is a comparison method that handles null safely. It returns true if both values are null, false if only one is null, and otherwise uses a’s equals method.
For primitive values, such as int numbers, == compares the values themselves. Primitive values are not object references.
Exercise
Two separate String objects both contain "cat". What do == and equals return when comparing them?
Show answer
== returns false because they are separate objects. equals returns true because their text matches.
Exercise
What does Objects.equals(null, null) return, and why?
Show answer
It returns true because both values are null.