Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Object class

Reference for java.lang.Object, the root of every Java class: equals, hashCode, toString, getClass, clone, finalize and wait/notify, with contracts.

java.lang.Object is the superclass of every class in Java. If a class declaration has no extends clause, it extends Object; arrays and enums do too. That means every object, whatever its type, can be compared with equals(), hashed with hashCode(), printed with toString() and inspected with getClass().

The default implementations are deliberately minimal: equals() is identity, hashCode() is derived from identity, and toString() prints the class name and a hash. Most classes you write should override the first three together, following their contracts.

Java
public class Object {
    public boolean equals(Object obj)
    public int hashCode()
    public String toString()
    public final Class<?> getClass()
}

Try it

Java Overriding equals, hashCode and toString

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java getClass and Object as a universal type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Method Description
boolean equals(Object obj) Whether this object is "equal to" obj. The default is this == obj.
int hashCode() An integer hash for hash-based collections.
String toString() A text form. Default: getClass().getName() + "@" + Integer.toHexString(hashCode()).
final Class<?> getClass() The exact runtime class.
protected Object clone() Field-by-field copy; throws CloneNotSupportedException unless the class implements Cloneable.
final void wait(), wait(long), notify(), notifyAll() Monitor-based thread coordination; the caller must hold the object's lock.
protected void finalize() Deprecated for removal since Java 18. Do not use; prefer try-with-resources or Cleaner.

The equals() contract: reflexive, symmetric, transitive, consistent, and x.equals(null) is false.

The hashCode() contract: stable while the fields used in equals() do not change, and equal objects must have equal hash codes. Unequal objects may share one.

Usage notes

  • Override equals() and hashCode() together. If you override only equals(), a HashSet or HashMap will treat equal objects as different keys.
  • equals(Object) takes Object. Writing equals(Point p) overloads rather than overrides, and @Override catches that mistake.
  • Use instanceof or getClass() deliberately. instanceof allows subclasses to equal their parents (and risks symmetry problems); comparing getClass() is stricter.
  • Do not print hash-based toString() output in tests or logs you need to compare: the default form includes an identity-derived hash that changes between runs.
  • Mutable keys. Changing a field used in hashCode() after inserting the object into a HashSet or as a HashMap key makes it unfindable.
  • Helpers: Objects.equals(), Objects.hash(), Objects.toString() handle null for you. Records and enums generate or fix these methods.
  • getClass() cannot be overridden and returns the runtime type, unlike obj.getClass() on a static type name.
  • clone() is awkward. Copy constructors and factory methods are usually clearer; see Cloneable.
  • wait()/notify() need the synchronized lock, otherwise IllegalMonitorStateException is thrown.