Java toString() Method

1. Overview

Every class in Java is a child of the Object class either directly or indirectly. And since the Object class contains a toString() method, we can call toString() on any instance and get its string representation.

In this tutorial, we’ll look at the default behavior of toString() and learn how to change its behavior.

2. Default Behavior

Whenever we print an object reference, it invokes the toString() method internally. So, if we don’t define a toString() method in our class, then Object#toString() is invoked.

Object’s toString() method is pretty generic:

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

To see how this works, let’s create a Customer object that we’ll use throughout our tutorial:

public class Customer {
    private String firstName;
    private String lastName;
    // standard getters and setters. No toString() implementation
}

Now, if we try to print our Customer object, Object#toString() will be called, and the output will be similar to:

com.maixuanviet.tostring.Customer@6d06d69c

3. Overriding Default Behavior

Looking at the above output, we can see that it doesn’t give us much information about the contents of our Customer object. Generally, we aren’t interested in knowing the hashcode of an object, but rather the contents of our object’s attributes.

By overriding the default behavior of the toString() method, we can make the output of the method call more meaningful.

Now, let’s look at a few different scenarios using objects to see how we can override this default behavior.

4. Primitive Types and Strings

Our Customer object has both String and primitive attributes. We need to override the toString() method to achieve a more meaningful output:

public class CustomerPrimitiveToString extends Customer {
    private long balance;

    @Override
    public String toString() {
        return "Customer [balance=" + balance + ", getFirstName()=" + getFirstName()
          + ", getLastName()=" + getLastName() + "]";
    }
}

Let’s see what we get when we call toString() now:

@Test
public void givenPrimitive_whenToString_thenCustomerDetails() {
    CustomerPrimitiveToString customer = new CustomerPrimitiveToString();
    customer.setFirstName("Rajesh");
    customer.setLastName("Bhojwani");
    customer.setBalance(110);
    assertEquals("Customer [balance=110, getFirstName()=Rajesh, getLastName()=Bhojwani]", 
      customer.toString());
}

5. Complex Java Objects

Let’s now consider a scenario where our Customer object also contains an order attribute that is of type Order. Our Order class has both String and primitive data type fields.

So, let’s override toString() again:

public class CustomerComplexObjectToString extends Customer {
    private Order order;
    //standard setters and getters
    
    @Override
    public String toString() {
        return "Customer [order=" + order + ", getFirstName()=" + getFirstName()
          + ", getLastName()=" + getLastName() + "]";
    }      
}

Since order is a complex objectif we just print our Customer object, without overriding the toString() method in our Order class, it will print orders as Order@<hashcode>.

To fix that let’s override toString() in Order, too:

public class Order {
    
    private String orderId;
    private String desc;
    private long value;
    private String status;
 
    @Override
    public String toString() {
        return "Order [orderId=" + orderId + ", desc=" + desc + ", value=" + value + "]";
    }
}

Now, let’s see what happens when we call the toString() method on our Customer object that contains an order attribute:

@Test
public void givenComplex_whenToString_thenCustomerDetails() {
    CustomerComplexObjectToString customer = new CustomerComplexObjectToString();    
    // .. set up customer as before
    Order order = new Order();
    order.setOrderId("A1111");
    order.setDesc("Game");
    order.setStatus("In-Shiping");
    customer.setOrders(order);
        
    assertEquals("Customer [order=Order [orderId=A1111, desc=Game, value=0], " +
      "getFirstName()=Rajesh, getLastName()=Bhojwani]", customer.toString());
}

6. Array of Objects

Next, let’s change our Customer to have an array of OrdersIf we just print our Customer object, without special handling for our orders object, it will print orders as Order;@<hashcode>.

To fix that let’s use Arrays.toString() for the orders field:

public class CustomerArrayToString  extends Customer {
    private Order[] orders;

    @Override
    public String toString() {
        return "Customer [orders=" + Arrays.toString(orders) 
          + ", getFirstName()=" + getFirstName()
          + ", getLastName()=" + getLastName() + "]";
    }    
}

Let’s see the results of calling the above toString() method:

@Test
public void givenArray_whenToString_thenCustomerDetails() {
    CustomerArrayToString customer = new CustomerArrayToString();
    // .. set up customer as before
    // .. set up order as before
    customer.setOrders(new Order[] { order });         
    
    assertEquals("Customer [orders=[Order [orderId=A1111, desc=Game, value=0]], " +
      "getFirstName()=Rajesh, getLastName()=Bhojwani]", customer.toString());
}

7. Wrappers, Collections, and StringBuffers

When an object is made up entirely of wrappers, collections, or StringBuffers, no custom toString() implementation is required because these objects have already overridden the toString() method with meaningful representations:

public class CustomerWrapperCollectionToString extends Customer {
    private Integer score; // Wrapper class object
    private List<String> orders; // Collection object
    private StringBuffer fullname; // StringBuffer object
  
    @Override
    public String toString() {
        return "Customer [score=" + score + ", orders=" + orders + ", fullname=" + fullname
          + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + "]";
    }
}

Let’s again see the results of calling toString():

@Test
public void givenWrapperCollectionStrBuffer_whenToString_thenCustomerDetails() {
    CustomerWrapperCollectionToString customer = new CustomerWrapperCollectionToString();
    // .. set up customer as before
    // .. set up orders as before 
    customer.setOrders(new Order[] { order }); 
    
    StringBuffer fullname = new StringBuffer();
    fullname.append(customer.getLastName()+ ", " + customer.getFirstName());
    
    assertEquals("Customer [score=8, orders=[Book, Pen], fullname=Bhojwani, Rajesh, getFirstName()=Rajesh, "
      + "getLastName()=Bhojwani]", customer.toString());
}

8. Conclusion

In this article, we looked at creating our own implementations of the toString() method.

All of the source code for this article is available over on GitHub.

Related posts:

Convert a Map to an Array, List or Set in Java
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to Implement Quick Sort with Given Complexity Constraint
Creating a Custom Starter with Spring Boot
Control the Session with Spring Security
Find the Registered Spring Security Filters
Java Concurrency Interview Questions and Answers
Spring Boot - Internationalization
Spring Boot - Enabling HTTPS
Debug a HttpURLConnection problem
Introduction to PCollections
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement Trie
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Implement Splay Tree
Vector trong Java
Java Program to Implement Radix Sort
Getting Started with Forms in Spring MVC
Java Program to Implement Bucket Sort
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Hướng dẫn Java Design Pattern – Factory Method
Java Program to Decode a Message Encoded Using Playfair Cipher
Hướng dẫn Java Design Pattern – Null Object
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement Knight’s Tour Problem
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Implement Gaussian Elimination Algorithm
HttpClient 4 – Send Custom Cookie
Java Program to Implement Queue using Two Stacks
Spring Boot - Rest Controller Unit Test