Java ToString() Method
The toString() method in Java is fundamental to representing an object as a string. As the name implies, this method converts an object into a string and returns its string representation. Because the Object class is the parent class of all Java classes, every Java class, directly or indirectly, inherits it.
The toString() method, a member of the Object class, holds significance in debugging, logging, printing to the console, serializing objects, and acquiring information about an object. This article explores the purpose, usage, and importance of Java’s toString() method, highlighting its role in enhancing code readability and facilitating effective debugging.
What does the toString() method In Java do?
As the name implies, the toString method in Java converts an object to a string and then returns it. This method comes in handy when you want to represent an object as a string, as it returns the object’s String representation.
The Object class is located in the java.lang package and is the parent class of all Java classes. This means that every Java class is either directly or indirectly derived from the Object class.
If a class does not extend another class, it is a direct Object subclass. If it extends another class, it is indirectly derived from Object. As a result, all Java classes have access to the methods of the Object class.
The toString() method is a method that is used to get the string representation of an object. It is typically used in debugging and logging. The toString() method is invoked whenever we try to print the object reference.
If we do not define the toString() method in our class, then the Object class’s toString() method will be invoked. Our implemented or overridden toString() method will be invoked, otherwise. The toString() method is a very important method in Java.
This method is used in many different places, such as printing an object to the console, serializing an object, and getting information about an object. It is essential to ensure that your toString() method returns a meaningful string representation of your object.
How to use the toString method() in Java?
Before we look at an example of a code that uses the toString() method, let us look at a code that does not use this method so that we understand what the problem is and that the toString() method comes in handy.
class FirstCodeEmployee { int idno; String name; String city; FirstCodeEmployee(int idno, String name, String city) { this.idno=idno; this.name=name; this.city=city; } public static void main(String args[]) { FirstCodeEmployee emp1=new FirstCodeEmployee(001,"Gopi","Bangalore"); FirstCodeEmployee emp2=new FirstCodeEmployee(002,"Ganesh","Hyderabad"); System.out.println(emp1); System.out.println(emp1); } }
Output:
FirstCodeEmployee@5fdef03a
FirstCodeEmployee@3b22cdd0
The compiler prints out gibberish in the output screen, which is nothing but the hash codes that refer to the objects created. Since the Java compiler internally calls the toString() method, overriding this method will return the specified values. This brings us to an example of overriding the toString() method.
class FirstCodeEmployee { int idno; String name; String city; FirstCodeEmployee(int idno, String name, String city) { this.idno=idno; this.name=name; this.city=city; } public String toString() {return idno+" "+name+" "+city;} public static void main(String args[]) { FirstCodeEmployee emp1=new FirstCodeEmployee(001,"Gopi","Bangalore"); FirstCodeEmployee emp2=new FirstCodeEmployee(002,"Ganesh","Hyderabad"); System.out.println(emp1); System.out.println(emp2); } }
Output:
101 Gopi Bangalore
002 Ganesh Hyderabad
In the example code shown above, the Java compiler internally calls the toString() method, However, by overriding this method the toString() function returns the specified values of emp1 and emp2 objects of FirstCodeEmployee class.
Conclusion
In summary, Java’s toString() method is a fundamental tool for converting object instances into meaningful string representations. By understanding and implementing the toString() method, developers can improve code readability and simplify debugging. The examples demonstrate how overriding this method can create customized and informative string representations, making it a valuable asset in Java programming.