Sub topic 8
What `toString` Is For and What the Default Prints
This chapter assumes you can write Java classes, create objects, and override methods. You should also know that every Java class ultimately extends java.lang.Object.
The purpose of `toString`
toString returns a text representation of an object. Java calls it when an object must be shown as text, such as when you pass the object to System.out.println or join it with a string.
class User {
private final String name;
User(String name) {
this.name = name;
}
}
User user = new User("Mina");
System.out.println(user);
System.out.println("User: " + user);If User does not override toString, both statements print a representation inherited from Object. The output resembles this:
User@5e2de80c
User: User@5e2de80cThe exact hexadecimal value varies between runs.
What the default output means
The default Object.toString result has this general form:
fully.qualified.ClassName@hexadecimalHashCodeFor a class named User in the package example, the result might be:
example.User@5e2de80cThe value after @ is the hexadecimal form of the object's hashCode() result. It is not a list of the object's fields, and it is not a reliable identifier for the object. Two different objects can have the same hash code.
The default representation is useful for identifying the class involved, but it usually does not help you understand the object's state.
Why override it
A useful toString includes the fields that help a developer inspect the object.
public final class User {
private final String name;
private final int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{name='" + name + "', age=" + age + "}";
}
public static void main(String[] args) {
User user = new User("Mina", thirty);
System.out.println(user);
}
}The example contains an invalid name, thirty, because Java requires an integer. The corrected complete version is:
public final class User {
private final String name;
private final int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{name='" + name + "', age=" + age + "}";
}
public static void main(String[] args) {
User user = new User("Mina", 30);
System.out.println(user);
}
}It prints:
User{name='Mina', age=30}The @Override annotation asks the compiler to verify that the method really overrides Object.toString. It catches mistakes such as writing toString(int) instead.
Use toString for diagnostics, logs, and readable development output. Do not treat its format as a stable data format unless you explicitly design and document it. Avoid including passwords, access tokens, or other sensitive values.
Arrays are another common surprise. An array inherits Object.toString, so printing an int[] produces text like [I@6d03e736. Use Arrays.toString(values) for a readable one-dimensional array.
Exercise
What does the default toString output tell you, and why should a value class usually override it?
Show answer
It shows the runtime class name and a hexadecimal hash-code value, but not the fields. A value class should override it to provide useful diagnostic text.