Sub topic 1
Object: where equals, hashCode, and toString come from
Prerequisites
This chapter assumes that you can write and compile basic Java classes, declare fields and constructors, use object references, and distinguish primitive values from objects. You should also know that a subclass inherits methods from its superclass unless it overrides them.
Every class has an Object
java.lang.Object is the root class of the Java class hierarchy. If a class declaration does not name another superclass, it extends Object automatically.
public class Book {
private final String title;
public Book(String title) {
this.title = title;
}
}The declaration above is equivalent to:
public class Book extends Object {
private final String title;
public Book(String title) {
this.title = title;
}
}Because Book inherits from Object, every Book object has methods such as equals, hashCode, and toString. These methods provide general behavior, but their default behavior may not match the meaning of your class.
The default methods
The default equals(Object) method compares object identity. It returns true only when both references point to the same object. Two objects with identical field values are still different objects.
The default hashCode() returns an integer associated with the object. The exact value is not a useful description of the object, and different objects may have the same hash code.
The default toString() returns a text form containing the runtime class name, an @ character, and a hexadecimal hash-related value. For example, it might look like Book@5acf9800. This output is useful for identifying an object during debugging, but it does not show the object's fields.
public class Main {
public static void main(String[] args) {
Object first = new Book("Java");
Object second = new Book("Java");
System.out.println(first == second); // false
System.out.println(first.equals(second)); // false
System.out.println(first.getClass()); // class Book
System.out.println(first); // implementation-dependent text
}
}
class Book {
private final String title;
Book(String title) {
this.title = title;
}
}The two Book instances contain the same title, but Book has not defined what “equal” means. Therefore, it inherits identity-based equality from Object.
getClass() reports the object's runtime class. It is also inherited from Object and is useful when inspecting an unknown reference.
Why override these methods?
A value class represents data whose contents determine its meaning. For example, two Point objects with coordinates (2, 3) usually represent the same value. Such a class should override equals to compare relevant fields, hashCode to calculate a matching value-based hash, and toString to produce readable diagnostic text.
These methods are related. If equals says two objects are equal, their hash codes must also be equal. Collections such as HashSet and HashMap depend on that rule. The next chapters develop each contract and show how to implement the methods safely.
Exercise
Why does new Book("Java").equals(new Book("Java")) return false in the example?
Show answer
The Book class inherits Object.equals, which compares whether two references point to the same object rather than comparing the title fields.