toString and the Object methods

Every class inherits eleven methods from Object, and three of them are worth an override in every value class: toString, equals and hashCode. This lesson takes toString, which is the one a print or a debugger calls.

toString

println and string concatenation call toString on any object they are given. The inherited method writes the class name, an @ and a hexadecimal hash, which is useful for identity and useless for a reader. An override returns the fields that describe the object.

The file Player.java:

public class Player { private final String name; private final int score; Player(String name, int score) { this.name = name; this.score = score; } @Override public String toString() { return name + " (" + score + ")"; } public static void main(String[] args) { Player player = new Player("Ada", 42); System.out.println(player); System.out.println("value: " + String.valueOf(player)); System.out.println("concat: " + player); } }

javac -Xlint:all Player.java
java Player

Ada (42)
value: Ada (42)
concat: Ada (42)

println, String.valueOf and the concatenation all end in the same call, which is why the three lines agree. The override may not return null: the callers expect a string, and a null from toString is a bug in the class.

The default toString

Without an override the output is Player@1a2b3c, where the number is the identity hash in hexadecimal. It changes between runs, so this page does not print it: a value that differs on every run is not an output a lesson can show. It is still worth knowing the form, because it is what a missing override looks like in a log.

getClass

getClass returns the runtime class of an object as a Class object. Its name is available as the full name with the package and as the simple name, and the class object can be compared with a class literal such as Player.class.

public class ClassInfo { public static void main(String[] args) { Player player = new Player("Ada", 42); System.out.println("name: " + player.getClass().getName()); System.out.println("simple: " + player.getClass().getSimpleName()); System.out.println("same: " + (player.getClass() == Player.class)); } } class Player { private final String name; private final int score; Player(String name, int score) { this.name = name; this.score = score; } }

javac -Xlint:all ClassInfo.java
java ClassInfo

name: Player
simple: Player
same: true

The two names are equal here because the class is in the unnamed package. A class in a package would give demo.Player for the full name and Player for the simple one, which is the reason getSimpleName exists.

A debug line

A toString that names the class and the fields is the first debugging tool a class has: a print of the object shows what it holds, a collection prints its elements with their fields, and a log line is readable without a debugger. Keep it free of side effects and of secrets, and let it show the fields that identify the object rather than every field.

Exercises

  1. Change the override in Player.java to return only the name and compare the three output lines.
  2. Remove the override and print the object. What is the form of the line, and why does it change between runs?
  3. Give ClassInfo.java a second class in a package? Do it in a folder with a package declaration and compare the two names.
  4. Add getClass().getSimpleName() to the toString of Player and run it again.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which method does println call on an object?

    Answer

    toString

    PrintStream.println calls String.valueOf, which calls toString for a non-string object.

  2. What does the default toString return?

    Answer

    The class name, an @ and the identity hash in hexadecimal

    Object.toString returns getClass().getName() + "@" + Integer.toHexString(hashCode()).

  3. What does getClass().getSimpleName() return?

    Answer

    The class name without the package

    getSimpleName drops the package, while getName keeps the full name.

  4. What does this program print? public class Q1 { public static void main(String[] args) { P p = new P(3); System.out.println(p); } } class P { int value; P(int value) { this.value = value; } @Override public String toString() { return "P(" + value + ")"; } }

    Answer

    P(3)

    println calls the override, which writes P(3).

  5. What does this program print? public class Q2 { public static void main(String[] args) { P p = new P(); System.out.println("x" + p); } } class P { @Override public String toString() { return "p"; } }

    Answer

    xp

    The concatenation calls toString and joins the two strings without a space.

  6. What does this program print? public class Q3 { public static void main(String[] args) { System.out.println(String.valueOf(new P())); } } class P { @Override public String toString() { return "value"; } }

    Answer

    value

    String.valueOf calls toString on the object.

  7. What does this program print? public class Q4 { public static void main(String[] args) { P p = new P(); System.out.println(p.getClass() == P.class); } } class P { }

    Answer

    true

    The runtime class of the object is P, and the class literal P.class is the same Class object.

  8. What should a toString override not return?

    Answer

    null

    Callers expect a string, and a null from toString breaks println and concatenation.

  9. What does this program print? public class Q5 { public static void main(String[] args) { P a = new P(); P b = new P(); System.out.println(a.equals(b)); } } class P { }

    Answer

    false

    Without an equals override, two different objects are unequal.

  10. Why is the default toString useless in a log?

    Answer

    It shows the identity hash rather than the state

    The class name and hash say which object it is, not what it holds.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev