Java toString() method

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()); }
Advantage of Java toString() method

By overriding the toString() method of the Object class, we can return values of the object, so we don't need to write much code.

Example without toString() method

Simple code that prints reference.

class Student { int rollno; String name; String city; Student(int rollno, String name, String city) { this.rollno=rollno; this.name=name; this.city=city; } public static void main(String args[]){ Student s1=new Student(131,"pavi","tirupati"); Student s2=new Student(136,"Rama","Tirupat"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } }


Output:
Student@3e25a5 Student@19821f

As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values.

// 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("Rama", 35, "SITECH", "M.C.A", "Tirupati"); System.out.println(b); System.out.println(b.toString()); } }


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.

Example of Java toString() method
class Student{ int rollno; String name; String city; Student(int rollno, String name, String city){ this.rollno=rollno; this.name=name; this.city=city; } public String toString(){//overriding the toString() method return rollno+" "+name+" "+city; } public static void main(String args[]){ Student s1=new Student(131,"Pavi","Tirupati"); Student s2=new Student(136,"Rama","Tirupati"); System.out.println(s1);//compiler writes here s1.toString() System.out.println(s2);//compiler writes here s2.toString() } }


Output:
131 Pavi Tirupati 136 Rama Tirupati
// 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("Rama", 35, "SITECH", "M.C.A", "Tirupati"); System.out.println(b); System.out.println(b.toString()); } }


Output:
Rama 35 SITECH M.C.A Tirupati Rama 35 SITECH M.C.A Tirupati

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.




Instagram