Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

The toString() method in Java

Reference for Java toString(): the default Object output, overriding it for readable logs and debugging, records, enums, arrays, and string conversion rules.

toString() returns a text description of an object. It is called implicitly by string concatenation, String.valueOf, println, printf("%s"), and by collections when they print their elements. The version in Object returns the class name, an @ and a hexadecimal hash code, which is almost never useful. Overriding it gives you readable logs, error messages and debugger output.

A good toString is short, describes the state that identifies the object, and never throws. It is meant for humans and diagnostics, not as a stable serialization format.

Java
@Override
public String toString() {
    return "Point[x=" + x + ", y=" + y + "]";
}

Try it

Java Overriding 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 Records, enums, arrays and null

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

Java
public String toString()
Detail Description
Returns The string form of the object. Should not be null.
Throws Nothing by convention; avoid doing I/O or calling code that can fail.

Related conversion helpers:

Method Behavior
String.valueOf(Object) "null" for null, else toString().
Objects.toString(o) Same as String.valueOf(o).
Objects.toString(o, fallback) Returns fallback when o is null.
Arrays.toString(array) Contents of a one-dimensional array.
Arrays.deepToString(array) Contents of nested arrays.

Usage notes

  • + calls toString() for you, and prints null for a null reference. Calling obj.toString() on a null reference throws NullPointerException.
  • Arrays do not override toString(); System.out.println(intArray) prints something like [I@1b6d3586. Use Arrays.toString.
  • Records get a toString of the form Name[component=value, ...]. Enums print their constant name unless you override it; use name() when you need the identifier regardless of overrides.
  • Avoid cycles. If a parent prints its children and each child prints its parent, you get a StackOverflowError. Print an id instead of the full neighbor.
  • Do not leak secrets. Passwords, tokens and personal data should be left out, because toString ends up in logs.
  • Do not parse it. Its format is not part of your API contract, so callers should not depend on the exact text. Offer a dedicated formatting method if you need one.
  • Cost: it is called whenever a log statement builds a message, even if that level is disabled, unless you use a lazy logging API. Keep it cheap.
  • Inheritance: a subclass can extend the parent text with super.toString(), but only if the parent override is meaningful (not Object's).
  • Text blocks and formatted() make multi-line descriptions tidy, though one line is usually better for logs.