The toString() method of the Object class in Java

Massimo Caliman
1 min readJun 13, 2023

--

The toStringmethod is a fundamental method of the Object class in Java. This method returns a textual representation of the object.

To use the String.toString() correctly, it is important to follow some rules. First, the toString method must be appropriately overridden in subclasses. Additionally, the textual representation returned by the toString method should be clear and useful for understanding the state of the object.

If the toString method is not overridden in a subclass, its default implementation in the Object class will return a string representing the name of the object’s class followed by the @ symbol and the object’s hash code in hexadecimal.

Here is an example of how to override toString in a subclass:

public class Human {
private String firstName;
private String lastName;
private String passportNumber;
public Human(String firstName, String lastName, String passportNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.passportNumber = passportNumber;
}
@Override
public String toString() {
return "Human{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", passportNumber='" + passportNumber + '\'' +
'}';
}
}

In this example, we have overridden the toString method in the `Human` class to return a textual representation of a Human object that includes its `firstName`, `lastName`, and `passportNumber` attributes.

Always remember to override the toString method, it costs little and can help you with debugging and logging!

You can find this post and others on my blog: trueprogramming.com

--

--

Massimo Caliman
Massimo Caliman

Written by Massimo Caliman

Software Architect, Developer, Java enthusiast

No responses yet