Unravel โบ Java concepts โบ OOP and types
The Object class: toString, getClass, hashCode
Every class extends Object and inherits toString, equals, hashCode and getClass.
Learn these first
At a glance
Every product leaves the factory with a default label printed by the system (class name and a serial number).
- 1toString() is called implicitly by string concatenation ("x" + obj), println(obj), and logging with {} placeholders
- 2getClass() returns the runtime class (obj.getClass().getSimpleName()), which can differ from the variable typeIt is also what equals often compares.
- 3== compares references (is it the very same object); equals compares meaning, if the class overrides it; hashCode must agree with equals
- Do not put secrets (passwords, tokens) into toString: it ends up in logs
- The default hashCode is identity-based and unrelated to the field values
- The default toString is getClass().getName() + "@" + Integer.toHexString(hashCode())
public class Account { private final String id; private final long cents; Account(String id, long cents) { this.id = id; this.cents = cents; } @Override public String toString() { return getClass().getSimpleName() + "{id=" + id + ", cents=" + cents + "}"; }}- line 5 uses getClass, the core of The Object class: toString, getClass, hashCode
Read the full explanation (about 1 min)
Every class extends Object and inherits toString, equals, hashCode and getClass. Overriding toString is what makes an object print as Order{id=7} instead of Order@1b6d3586.
A mental model
Every product leaves the factory with a default label printed by the system (class name and a serial number). toString lets you print a label that humans can actually read.
How it works
toString() is called implicitly by string concatenation ("x" + obj), println(obj), and logging with {} placeholders.
getClass() returns the runtime class (obj.getClass().getSimpleName()), which can differ from the variable type. It is also what equals often compares.
== compares references (is it the very same object); equals compares meaning, if the class overrides it; hashCode must agree with equals.
Going deeper
Do not put secrets (passwords, tokens) into toString: it ends up in logs. Do not call collections or lazy-loaded relations in it either, or printing an entity can trigger database queries or infinite recursion between two objects that print each other.
The default hashCode is identity-based and unrelated to the field values. Overriding equals without hashCode breaks HashMap and HashSet.
finalize() is deprecated for removal: it is unpredictable and slow. Use try-with-resources or Cleaner.
Under the hood
The default toString is getClass().getName() + "@" + Integer.toHexString(hashCode()). The identity hash is generated lazily by the JVM and stored in the object header.
Common mistakes
- Comparing strings or boxed numbers with
==. - Logging an object that has no
toString. - Including sensitive or lazy data in
toString.
How Unravel recognises it
When you paste code, Unravel looks for these signs of the object class: tostring, getclass, hashcode. Spotting them yourself is a good habit when you read unfamiliar code:
- Method calls:
getClass()getSimpleName()getName()identityHashCode() - Method names you declare:
toString()finalize()
Part of a learning path
- Object-oriented Java in depth ยท previous: Immutability and final classes ยท next: Copying objects: clone and copy constructors
Related concepts
Frequently asked questions
What is The Object class: toString, getClass, hashCode?
Every class extends Object and inherits toString, equals, hashCode and getClass. Overriding toString is what makes an object print as Order{id=7} instead of Order@1b6d3586.
What are the most common mistakes with The Object class: toString, getClass, hashCode?
Comparing strings or boxed numbers with ==. Logging an object that has no toString. Including sensitive or lazy data in toString.
How can I see The Object class: toString, getClass, hashCode in real code?
Paste your own Java into Unravel and it will point out where the object class: tostring, getclass, hashcode appears, explain how it works at the detail level you choose, and draw the flow of the code around it.