Purpose of Overriding toString() Method in Java

Amir Ghahrai

Amir Ghahrai

Follow on Twitter

What is the purpose of toString() method in Java?

If we want to represent an object of a class as a String, then we can use the toString() method which returns a textual representation of the object.

When you print an object, by default the Java compiler invokes the toString() method on the object. So by overriding the toString() method, we can provide meaningful output.

Let’s see this concept in the following example:

Overriding toString() Method

package io.devqa.tutorials;

public class ToStringExample {

    private String firstName;
    private String lastName;
    private String email;

    public ToStringExample() {


    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("First name : ").append(this.firstName).append("\n");
        sb.append("Last name : ").append(this.lastName).append("\n");
        sb.append("Email : ").append(this.email).append("\n");
        return sb.toString();

    public static void main(String args[]) {
        ToStringExample example = new ToStringExample();


The above code outputs the following:

First name : Testing
Last name : Tester
Email : testing@tester.com

If we didn’t override the toString() method, the output would have been


As can be seen, by overriding the toString() method, we can output meaningful presentation of the object.