toString() method

This method returns the string representation of the object which invokes this method.

This method is mainly used to represent the java object into a meaningful string.

This is mostly required while logging the object information into logger file.

This method is defined in Object class and any other class in java(apparently subclass of Object class) can override this method.


Signature of this method

  1. public String toString()
public String toString()

If we don’t override this method in our class, then it returns the Object’s address in hexadecimal format along with class name.

Example:

  1. public class Employee1{
  2. String name;
  3. int age;
  4. public static void main(String args[])
  5.     {
  6.         Employee1 e = new Employee1 ();  
  7.         e.name=”john”;
  8.         e.age=20;
  9.         System.out.println(e);
  10.     }
  11. }
public class Employee1{
String name;
int age;
public static void main(String args[])
    {
        Employee1 e = new Employee1 ();  
        e.name=”john”;
        e.age=20;
        System.out.println(e);
    }
}



If we override this method in our class, then it returns the value provided in overridden toString() method

Example

  1. public class Employee2{
  2. String name;
  3. int age;
  4. public static void main(String args[])
  5.     {
  6.         Employee2 e = new Employee2 ();  
  7. e.name=”john”;
  8. e.age=20;
  9.         System.out.println(e); //same as  System.out.println(e.toString());
  10.     }
  11. public String toString()
  12.     {
  13.         return "name->"+name+”  “+”age->+age;
  14.     }
  15. }
public class Employee2{
String name;
int age;
public static void main(String args[])
    {
        Employee2 e = new Employee2 ();  
e.name=”john”;
e.age=20;
        System.out.println(e); //same as  System.out.println(e.toString());
    }
public String toString()
    {
        return "name->"+name+”  “+”age->”+age;
    }
}


As we can see in the above example that, Printing an object gives a meaningful string which represent the state of an Employee object.

  1. System.out.println(e)
System.out.println(e)


This is same as

  1. System.out.println(e.toString());
System.out.println(e.toString());


Whenever we print any object of any class, its toString() method will be called

We can also call this explicitly for String class as below

  1. String str=”java”;
  2. System.out.println(str.toString());
String str=”java”;
System.out.println(str.toString());


This is same as

  1. String str=”java”;
  2. System.out.println(str);
String str=”java”;
System.out.println(str);


Since String class has already overridden the toString() method, it returns the string value rather than hexadecimal address of object.

Note :
Its good practice to override the toString() method in all our model classes which represent the state so that we can print them in the logger wherever required just by using object.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on