Object toString() Method in Java

Every class in java is child of Object class either directly or indirectly. Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked. If we did not define toString() method in your class then Object class toString() method is invoked otherwise our implemented/Overridden toString() method will be called.

Syntax of Object class toString() method:

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

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// working of toString() method
class Best_Friend {
    String name;
    int age;
    String college;
    String course;
    String address;
    Best_Friend
    (String name, int age, String college, String course, String address)
    {
        this.name = name;
        this.age = age;
        this.college = college;
        this.course = course;
        this.address = address;
    }
public static void main(String[] args)
    {
        Best_Friend b = 
        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
        System.out.println(b);
        System.out.println(b.toString());
    }
}

chevron_right


Output:

Best_Friend@232204a1
Best_Friend@232204a1

Explanation:In the above program, we create an Object of Best_Friend class and provide all the information of a friend. But when we try to print the Object, then we are getting some output which is in the form of classname@HashCode_in_Hexadeciaml_form. If we want the proper information about Best_friend object, then we have to override toString() method of Object class in our Best_Friend class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// working of toString() method
class Best_Friend {
    String name;
    int age;
    String college;
    String course;
    String address;
    Best_Friend
    (String name, int age, String college, String course, String address)
    {
        this.name = name;
        this.age = age;
        this.college = college;
        this.course = course;
        this.address = address;
    }
public String toString()
    {
        return name + " " + age + " " + college + " " + course + " " + address;
    }
public static void main(String[] args)
    {
        Best_Friend b = 
        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
        System.out.println(b);
        System.out.println(b.toString());
    }
}

chevron_right


Output:

Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu

NOTE:In all wrapper classes, all collection classes, String class, StringBuffer, StringBuilder classes toString() method is overriden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// working of toString() method
import java.util.*;
class Best_Friend {
    String name;
    int age;
    String college;
    String course;
    String address;
    Best_Friend
    (String name, int age, String college, String course, String address)
    {
        this.name = name;
        this.age = age;
        this.college = college;
        this.course = course;
        this.address = address;
    }
public static void main(String[] args)
    {
        Best_Friend b = 
        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
        System.out.println(b);
        String s = new String("Gulpreet Kaur");
        System.out.println(s);
        Integer i = new Integer(21);
        System.out.println(i);
        ArrayList l = new ArrayList();
        l.add("BIT");
        l.add("M.TECH");
        System.out.println(l);
    }
}

chevron_right


Output:

Best_Friend@232204a1
Gulpreet Kaur
21
[BIT, M.TECH]

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up