In Java, “toString()” is a non-static, built-in method that belongs to the root class. By adding this method to the root class, the users can use the “toString()” method in all the other classes. It returns the string representation of the objects which are not already in the form of string. This method makes it easy to visually examine the object by converting them to string. By default, the “toString()” method is invoked by the Java compiler whenever the “System.out.println()” method is used.
How to Use the “toString()” in Java?
We can use the “toString()” method in three different ways:
- “toString()” to be called as an object instance method
- By passing a value of the argument
- By overriding the “toString()” method
Method 1: “toString()” to be called as an Object Instance Method
The “toString()” method can be used in the program as an object instance. The instance specifies the particular representation of the object which enables users to represent it as a string. Check the below-stated code:
public class student_information{
public static void main(String[] args) {
Integer reg_no=10;
String name="Sara";
System.out.println(reg_no.toString());
System.out.println(name.toString());
}
}
In the above-provided code:
- First, we declare the “student_information” class.
- In the “main()” method, there is an integer type variable named “reg_no” and a string variable called “name”. Both variables are initialized while declaring.
- Then, print the values of these variables by using the “toString()” as a function of these variables inside the “System.out.println()” method.
Output

Method 2: Passing a Value of the Argument
Another efficient way for using the “toString()” is by passing the value of the argument inside this method. For instance, here we take the previous example. But this time we haven’t assigned the values at the time of initialization instead we have assigned the value inside the “toString()” function:
public class student_information{
Integer reg_no;
public static void main(String[] args) {
System.out.println(Integer.toString(5));
}
}

Method 3: By Overriding the “toString()” Method
In Java, the above-described method can be used by overriding the “toString()” method in the program. It helps users to get the specified values in the form of a string. The method will be overridden in the “main()” class and all the classes can access it. Here is the practical demonstration of the previously discussed concept:
public class student_information{
Integer reg_no;
String name;
student_information(Integer reg_no, String name){
this.reg_no=reg_no;
this.name=name;
}
public String toString(){
return "Reg no= "+reg_no+" ,Name= "+name;
}
public static void main(String[] args) {
student_information a=new student_information(25,"Ali");
student_information b= new student_information(30,"Sana");
System.out.println(a);
System.out.println(b);
System.out.println(Integer.toString(5));
}
}
Here:
- Initially, declare the “student_information” class that has two variables “reg_no” and “name” respectively.
- The return type of the “reg_no” variable is an integer and the “name” variable’s return type is a string.
- Then, add the declared class constructor that has two arguments. Inside the constructor definition, refer to the declared variables using the “this” keyword.
- Next, to show the values in the form of a string, override the “toString()” method along with the constructor parameters.
- After that, inside the “main()” method, we created the objects of the “student_information” class using the “new” keyword and its constructor. Then, assign the values to the provided variables, and print those values by calling the “System.out.println()” method.
Output

According to the above-described output, the values that we have stored in “a” and “b” are printed and also the specified value for the “Integer” by passing the value in the “toString()” method.
The question that arises in the user’s mind is, If we don’t override the “toString()” method in the program then what will get as an output. So, let’s have a look at the following program:
public class student_information{
Integer reg_no;
String name;
student_information(Integer reg_no, String name){
this.reg_no=reg_no;
this.name=name;
}
// public String toString(){
// return "Reg no= "+reg_no+" ,Name= "+name;
// }
public static void main(String[] args) {
student_information a=new student_information(25,"Ali");
student_information b= new student_information(30,"Sana");
System.out.println(a);
System.out.println(b);
System.out.println(Integer.toString(5));
}
}
Here, we can see that the provided output is not user readable when we remove the “toString()” method. However, the third output is readable as it was assigned using “toString()”.

Conclusion
In Java, the “toString()” method is used to convert the output into the form of a string so that it will be user readable. It can be utilized in different ways, such as by using it as an object instance, by using “toString()” to assign the values, and by overriding the “toString()” method in the “main()” method. We have briefly described the “toString()” method in Java.