BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

The Object class: toString, getClass, hashCode

Every class extends Object and inherits toString, equals, hashCode and getClass.

Learn these first

At a glance

Mental model

Every product leaves the factory with a default label printed by the system (class name and a serial number).

What happens, in order
  1. 1toString() is called implicitly by string concatenation ("x" + obj), println(obj), and logging with {} placeholders
  2. 2getClass() returns the runtime class (obj.getClass().getSimpleName()), which can differ from the variable typeIt is also what equals often compares.
  3. 3== compares references (is it the very same object); equals compares meaning, if the class overrides it; hashCode must agree with equals
Remember
Watch out
Comparing strings or boxed numbers with ==Logging an object that has no toStringIncluding sensitive or lazy data in toString
Example (highlighted lines explain it)
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 + "}";  }}
  1. 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:

Part of a learning path

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.