ToString Method in Java

In this article we will learn about one of the important methods, the toString() method, in Java with a basic illustration for a better understanding and explanation.

About toString() method

Basically it is a method that is used when we need a string representation of the object. It is defined in the object class. This method returns the string representation of the object.

The implementation of toString() method can be done by overriding the object's toString method. This method can be overridden to customize the string representation of the object.

When we print an object, the Java compiler internally invokes the toString() method and overrides it for the desired output.

Example without using toString() method

Let's see the example without using the toString() method and analyse the problem that occurs.

  1. class Employee  
  2. {  
  3.     int id,salary;  
  4.     String name;  
  5.     String city;  
  6.     Employee(int id, String name, int salary ,String city)  
  7.     {  
  8.         this.id=id;  
  9.         this.name=name;  
  10.         this.salary=salary;  
  11.         this.city=city;  
  12.     }  
  13.    public static void main(String args[])  
  14.    {  
  15.        Employee E1=new Employee(111,"Rakesh",50000,"Lucknow");  
  16.        Employee E2=new Employee(112,"Suresh",25000,"Kanpur");  
  17.        // using the default Object.toString method  
  18.        System.out.println("Employee details: "+E1);  
  19.        // implicitly calls the toString() method on object as a part of string concatenation  
  20.        System.out.println(E2+ " concatenation");  
  21.    }  
  22. }  
Output

Employee details: Employee@13e8d89
Employee@9664a1 concatenation

In the preceding example, you can see that we couldn't get the desired output since E1 and E2 prints the class name, “@” and the hex version of the object's hashcode is concatenated to a string. The default hashcode method in the object is typically implemented by converting a memory address of an object to an integer.

This all happens due to the Java compiler calling the toString() method and by overriding it returns the specified value.

Example using toString() method

Now let's have a look at the example that uses the toString() method.
  1. class Employee  
  2. {  
  3.     int id,salary;  
  4.     String name;  
  5.     String city;  
  6.     Employee(int id, String name, int salary ,String city)  
  7.     {  
  8.          this.id=id;  
  9.          this.name=name;  
  10.          this.salary=salary;  
  11.          this.city=city;  
  12.     }  
  13.     public String toString() // overrides toString() method  
  14.     {  
  15.         return id+" "+name+" "+salary+" "+city;  
  16.     }  
  17.     public static void main(String args[])  
  18.     {  
  19.         Employee E1=new Employee(111,"Rakesh",50000,"Lucknow");  
  20.         Employee E2=new Employee(112,"Suresh",25000,"Kanpur");  
  21.         // both will print Employee.toString()  
  22.         System.out.println("Employee details: "+E1);  
  23.         System.out.println("Employee details: "+E2);  
  24.      }  

Output

Employee details: 111 Rakesh 50000 Lucknow
Employee details: 112 Suresh 25000 Kanpur

Here, by using the toString() method we are able to represent the object as a string without any hashcode concatenated to it.

 

Up Next
    Ebook Download
    View all
    Learn
    View all