Open In App
Related Articles

Object toString() Method in Java

Like Article
Save Article
Report issue

Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object and if extends another class then it is indirectly derived. Therefore the Object class methods are available to all Java classes.

Note: Object class acts as a root of inheritance hierarchy in any java program

Now we will be dealing with one of its methods known as toString() method. We typically generally do use the toString() method to get the string representation of an object. It is very important and readers should be aware that whenever we try to print the object reference then internally toString() method is invoked. If we did not define the toString() method in your class then the Object class toString() method is invoked otherwise our implemented or overridden toString() method will be called.


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

Note: Default behavior of toString() is to print class name, then @, then unsigned hexadecimal representation of the hash code of the object.



// Java program to Illustrate
// working of toString() method
// Main class
class Best_Friend {
    // Member attributes of this class
    String name;
    int age;
    String college;
    String course;
    String address;
    // Constructor of this class
    Best_Friend(String name, int age, String college,
                String course, String address)
        // This variable refers to current instance itself = name;
        this.age = age; = college;
        this.course = course;
        this.address = address;
    // Method of this class
    // Main driver method
    public static void main(String[] args)
        // Creating an object of this class
        // Custom attributes been passed as in arguments
        Best_Friend b = new Best_Friend(
            "Gulpreet Kaur", 21, "BIT MESRA", "M.TECH",
        // Print and display commands to illustrate
        // toString() method as both will print the same
        // Print the object
        // Print the object but implicitly using toString()
        // method



Output 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 the Best_friend object, then we have to override the toString() method of the Object class in our Best_Friend class.

Example 2:



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 overridden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.

Example 3:



Output: It will also do throw out for warning of unchecked and unsafe operations

Gulpreet Kaur

Last Updated : 04 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads