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.
public class Object { public boolean equals(Object obj) public int hashCode() public String toString() public final Class<?> getClass() }
Try it
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.
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()andhashCode()together. If you override onlyequals(), aHashSetorHashMapwill treat equal objects as different keys. equals(Object)takesObject. Writingequals(Point p)overloads rather than overrides, and@Overridecatches that mistake.- Use
instanceoforgetClass()deliberately.instanceofallows subclasses to equal their parents (and risks symmetry problems); comparinggetClass()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 aHashSetor as aHashMapkey makes it unfindable. - Helpers:
Objects.equals(),Objects.hash(),Objects.toString()handlenullfor you. Records and enums generate or fix these methods. getClass()cannot be overridden and returns the runtime type, unlikeobj.getClass()on a static type name.clone()is awkward. Copy constructors and factory methods are usually clearer; see Cloneable.wait()/notify()need thesynchronizedlock, otherwiseIllegalMonitorStateExceptionis thrown.
Related
- String, Class, Record, Cloneable and clone(), == vs. equals()
- Lessons: The Object class, HashMap