Java ToString Method

At the core of the Java language is the concept of an Object. Every object created from a class extends from java.lang.Object. The java.lang.Object class provides a number of important methods.

In particular, in this tutorial we’ll examine the toString method of the java.lang.Object class.

ToString Default Behavior

If we don’t override the toString() method within a class, we’ll receive the default implementation of this method.

1
2
3
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Above we can see the default implementation of the ToString method from the Object class. It simply prints the class name followed by a hexadecimal representation of the objects hash code. Let’s see this in action.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class PersonName {
    private final String firstName;
    private final String middleName;
    private final String lastName;

    public PersonName(final String firstName,final String middleName,final String lastName) {
        AssertUtils.strNotNullAndNotEmpty(firstName, "First name is required");
        AssertUtils.strNotNullAndNotEmpty(middleName, "Middle name is required");
        AssertUtils.strNotNullAndNotEmpty(middleName, "Last name is required");

        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
    }
}

Above we create a class called PersonName, which contains the segments of a persons full name.

1
2
final PersonName personName = new PersonName("John", "Adam", "Doe");
System.out.println(personName);

If we execute the above code the call to System.out.println, will in turn call the toString method of the personName object. This will result in the following output.

1
com.stephenenright.tutorials.corejava.objects.ToStringExample$PersonName@6bdf28bb

As we can see the output is not very meaningful apart from the class name. Let’s see how we can improve on this outcome.

Overriding The ToString Method

We can provide a custom implementation of the toString method, by overriding it.

1
2
3
4
@Override
public String toString() {
    return String.format("First Name: %s, Middle Name: %s, Last Name: %s", firstName, middleName, lastName);
}

Above we override the ToString method to provide more meaningful output.

1
2
3
4
5
6
7
8
@Test
public void object_toString() {

    final PersonName personName = new PersonName("John", "Adam", "Doe");

    final String expected = "First Name: John, Middle Name: Adam, Last Name: Doe";
    assertEquals(expected, personName.toString());
}

As we can see the result is more informative. Overriding the toString method can be beneficial in use cases such as:

  • Displaying an Object
  • Debugging Java Programs
  • Application Logging

ToString With Object Compositions

Working with object graphs is no different than working with individual objects. For example, consider the below object composition.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static class Person {
    private final int id;
    private final PersonName name;

    public Person(final int id, final PersonName name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return String.format("Person Id: %s, Name: ", id, name);
    }
}

Above we create a Person class, that is a composition of an id and a PersonName. We also override the ToString method and output the id and the PersonName instance.

1
2
3
4
5
6
7
@Test
public void composition_toString() {
    final Person person = new Person(1,new PersonName("John", "Adam", "Doe"));

    final String expected = "Person Id: 1, Name: First Name: John, Middle Name: Adam, Last Name: Doe";
    assertEquals(expected, person.toString());
}

Above we can see that when we use the PersonName instance in the Person objects toString method it delegates to the PersonName toString method.

Conclusion

In this short tutorial we examined the ToString method in Java. We also briefly touched on some use cases where providing a custom implementation of this method is useful.