toString() method in java with example

toString() method is used to convert an object to its String representation. You can call this method on a String, a StringBuffer, a java.sql.Connection object or an object of your own custom class.
This method is available to all the classes because it is written in java.lang.Object class, which is the super class of all classes.

Default toString() method
You might have noticed that if you call toString() on an object, then it prints something like Student@4aa8fd04, where Student is the name of class.
This is because when you call toString() on an object, it invokes toString() method of java.lang.Object class by default, whose implementation is

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

This method will return the String representation of an object which is of the format

[ Class name ] + “@” + [ hash code of object converted to a String ]

Example,

public class Student {

   private String name;

   public static void main(String[] args) {
      // create student object
      Student student = new Student();
      // print it
      System.out.print(student);
   }
}

Above example creates an object of Student class and prints it. Below is the output produced

Student@4aa8fd04

Remember that the class name is a fully qualified name which means that the name of class is along with its package. Thus, if the class resides in package com.codippa, then the above output will be modified as

com.codippa.Student@4aa8fd04

You might have noticed that we have directly printed the object without explicitly invoking toString() method, still it prints the result in the format returned by toString() method.
This is because print method internally invokes toString() method on the object supplied to it as argument.
Use of toString()
As discussed above, toString() is used to convert an object to its String representation and is useful in below scenarios

    1. Suppose you override a method which returns a String. This method performs a large number of String operations so you choose a java.lang.StringBuffer.
      Since the return type of the method is a String, you need to convert StringBuffer to a String. This can be easily done by calling toString() on the StringBuffer object.
    2. You have your own class and you want a meaningful representation of its objects when they are printed. In this case, you can override toString() method in your class which is explained next.

Overriding toString() method
Overriding toString() method means providing a different implementation of this method in your class which is different from that given in java.lang.Object.
This is usually done so that when the object is converted to a String, a meaningful information is returned instead of class name and its hash code.
toString() is overridden in the same way as method overriding is performed in java. Example,

public class Student {

   private String name;

   public static void main(String[] args) {
      // create student object
      Student student = new Student();
      // set name
      student.name = "abc";
      // print it
      System.out.print(student);
   }

   @Override
   public String toString() {
      return "Student[ name= " + this.name + " ]";
   }
}

Above class overrides toString() method. When an object of this class is created and printed, following is the output

Student[ name = abc]

which is meaningful as compared to the default toString() output.
Notice the use of @Override annotation over toString() method. Using this annotation provides a compile time check and ensures that the method is overridden correctly.
toString() example in java
Many classes in java library provide an overridden toString() method so that their objects are printed in a meaningful way. Examples are
1. java.util.HashMap
When a hash map is printed, it produces an output of the form key=value.
2. java.util.ArrayList
An arraylist when printed prints all items contained in the list enclosed within square brackets.
3. java.io.File
When a file object is printed, the path of file is printed.
4. java.lang.Thread
A thread object also has an overridden toString() method which returns the name of thread and its priority. If the thread belongs to a group, then it is also printed.
There are many more examples of overridden toString() method implementation in java api classes.
Hope this article was useful in understanding the concept of toString() method in java. Click the clap icon if you liked it.

Leave a Reply