The Object class in Java is the root of the class hierarchy: every class extends java.lang.Object, directly or indirectly, so every object inherits its methods. The three you will override most often are toString(), equals(Object) and hashCode(). Getting them right is what makes your objects print readably, compare by value and work correctly as keys in a HashMap or elements of a HashSet.
This guide explains what the Object class gives you for free, why the default behaviour is rarely what you want, and how to write the three methods properly.
If you write class Book { } without an extends clause, the compiler treats it as class Book extends Object. That is why you can call methods on any reference without declaring them yourself, and why a variable of type Object can hold anything (except primitives, which are boxed first).
| Method | Purpose |
|---|---|
toString() | Text description of the object |
equals(Object) | Logical equality test |
hashCode() | Integer used by hash-based collections |
getClass() | The runtime class (final, cannot be overridden) |
clone() | Protected shallow copy, rarely used today |
wait(), notify(), notifyAll() | Low-level thread coordination |
finalize() also exists but has been deprecated for removal since Java 18. Use try-with-resources or Cleaner for cleanup instead.
The inherited toString() returns the class name, an @ sign and the hash code in hexadecimal, which tells you almost nothing.
The exact number changes between runs. Override toString() so logs and debuggers show something useful:
Now the same println prints Point[x=3, y=4]. System.out.println, string concatenation and logging frameworks all call toString() for you.
In Object, equals is the same as ==: two references are equal only if they point to the very same object.
For value-like classes you want a.equals(b) to be true when the fields match. That means overriding equals.
A reliable pattern, using pattern matching for instanceof (Java 16 and later):
Points to remember:
Object. Writing equals(Point p) overloads instead of overriding, and @Override will show the error.this == o first as a cheap shortcut.null because null instanceof Point is false.== for primitives and Objects.equals(a, b) for references, so that null fields do not throw.hashCode, as described next.Hash-based collections such as HashMap and HashSet call hashCode() first to choose a bucket and then equals() inside the bucket. If two objects are equal, they must return the same hash code. Always override both together.
Objects.hash is short and good enough for most classes. It allocates a small array, so for hot code you can write the arithmetic by hand, for example 31 * Integer.hashCode(x) + Integer.hashCode(y).
What happens when you forget:
With equals overridden but hashCode not, this usually prints false, because the two equal points land in different buckets. With both overridden it prints true.
Since Java 16 a record generates toString, equals and hashCode from its components:
new Point(3, 4) prints as Point[x=3, y=4] and two records with the same components are equal. For ordinary classes, IDEs such as IntelliJ IDEA and Eclipse can generate the three methods, and libraries like Lombok offer @EqualsAndHashCode. Use whichever your project already uses, but understand the rules so you can review the result.
Be careful with mutable fields in hashCode: if you change a field after putting the object into a HashSet, its bucket changes and the set can no longer find it. Prefer immutable key classes.
Because every class inherits from Object, which defines them. The defaults use the identity of the object, so you override them when you need a readable description or value-based equality.
Yes. The contract says equal objects must have equal hash codes. Breaking it makes HashMap and HashSet behave incorrectly.
== compares references for objects (and values for primitives). equals compares whatever the class defines, which for String, the wrapper classes and records means the contents.
getClass() is final, so you cannot override it. It returns the exact runtime class, which is useful in equals implementations that must reject subclasses.
The Object class sits at the top of every Java class hierarchy and supplies toString, equals, hashCode and several other methods. The defaults describe identity, so override toString for readable output, equals for value comparison and hashCode so that equal objects share a hash code. Use @Override, Objects.equals and Objects.hash, or let a record generate everything for immutable data.