What is toString Function in Java: The Ultimate Guide

Java is an object-oriented language, which means that all calculations are done on the object or objects that belong to the class. The toString Function in Java turns an object into a string.

Java’s toString() method is the default implementation for the Object class. The Object class, the parent class of all other classes, is in the Java Lang package. The toString() method is a standard part of Java, and all classes automatically get their default implementation.

The toString() method’s job is to give a string representation of the object it is called on. Either the technique provides a string with a description of the object, or it takes an integer and translates it into a string.

All JavaScript objects have access to the toString method. The function toString() in Javascript is called when the object must be shown as text (like in HTML) or used as a string.

In this post, we will cover the following pointers: 

  • Java’s toString() method
  • An Example of Java’s ToString() Method
  • Uses of Java toString()
  • Benefits of Java toString()
  • Array to toString() in Java Example
  • Override the toString() method in the Java example

The toString Function in Java : What Does it Exactly Imply?

If you need to turn any object into a string, you can use the toString() method.

The toString() method can get the object’s string representation.

When you tell the Java compiler to print an object, it automatically calls the Object’s toString() function. Therefore, changing the function toString() method could give you the output you want, which could be the current state of an object, depending on how you set it up.

An Example of toString Function in Java

toString Function in Java

Let’s look at how the toString() function is used to get a more straightforward understanding.

Student.java

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(101,”Laxmi”,”Delhi”);  

   Student s2=new Student(102,”Raju”,”Gujarat”);  

   System.out.println(s1);//compiler writes here s1.toString()  

   System.out.println(s2);//compiler writes here s2.toString()  

 }  

}  

Output: 

101: Laxmi Delhi

102: Raju Gujarat

Interpretation of toString Function in Java:

In the program above, the Java compiler calls the toString() method internally. If you override this method, it will return the values of the s1 and s2 objects of the Student class that you specify.

Uses of Java toString()

  • You can use the toString() method to return a string to how it was before.
  • You can be sure the toString() method will return the exact string you gave.
  • You can use the function toString() to get a string from a string object.

Benefits Of toString Function in Java

The Java function toString() is helpful in many ways. For example,

  • Since the toString() method is a Java base Object class component, any class can use it without doing any additional work. 
  • Any object that needs to have a string representation can use the method.
  • This can help you learn more about an object when debugging. 
  • Sometimes, you must change how a method is put into action. It’s a plus when working with Java code either way.

Array to toString() in Java Example

  • In the string representation, the elements of the array are shown in square brackets (“]”). The array passed in will be turned into a string, using a comma and a space to separate items close to one another. If an entry is empty, it returns null.
  • If an Object Array has other arrays as its elements, the Object.toString() method inherited from Object will stringify them so that their names are shown instead of their contents.
  • One of the possible parameters is arr, which is the array whose string representation should be returned.
  • It gives back the string form of arr.

Below is Java code that shows how the toString() function of the Arrays class works.

// Java program to demonstrate the working of Arrays.toString()

import java.io.*;

import java. util.*;

class GFG {

public static void main(String[] args)

{

// Let us create different types of arrays and

// print their contents using Arrays.toString()

boolean[] boolArr = new boolean[] { true, true, false, true };

byte[] byteArr = new byte[] { 10, 20, 30 };

char[] charger = new char[] { ‘g,’ ‘e,’ ‘e,’ ‘k,’ ‘s’ };

double[] dblArr = new double[] { 1, 2, 3, 4 };

float[] floater = new float[] { 1, 2, 3, 4 };

int[] int Arr = new int[] { 1, 2, 3, 4 };

long[] lomgArr = new long[] { 1, 2, 3, 4 };

Object[] objArr = new Object[] { 1, 2, 3, 4 };

short[] shortArr = new short[] { 1, 2, 3, 4 };

System.out.println(Arrays.toString(boolArr));

System.out.println(Arrays.toString(byteArr));

System.out.println(Arrays.toString(charArr));

System.out.println(Arrays.toString(dblArr));

System.out.println(Arrays.toString(floatArr));

System.out.println(Arrays.toString(intArr));

System.out.println(Arrays.toString(lomgArr));

System.out.println(Arrays.toString(objArr));

System.out.println(Arrays.toString(shortArr));

}

}

Output 

[true, true, false, true]

[10, 20, 30]

[g, e, e, k, s]

[1.0, 2.0, 3.0, 4.0]

[1.0, 2.0, 3.0, 4.0]

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4]

An Example of Overriding the toString() Method in Java

Java’s function toString() returns a string that represents the object. To return the object’s values, this function is overridden.

Here’s a program to understand this principle:

class Student {

   private int roll;

   private String name;

   public Student(int r, String n) {

      no = r;

      name = n;

   }

   public String toString() {

      return rollno + ” ” + name;

   }

}

public class Demo {

   public static void main(String[] args) {

      Student s = new Student(1001, “Shakira”);

      System.out.println(“The student details are:”);

      System.out.println(s);

   }

}

Output

The student details are:

1001 Shakira

Conclusion

To sum up, the toString() method in Java is a valuable function that lets developers give a string representation of an object that helps everyone. This method is part of the programming language called Java. The default return value of toString() is a string with the class name and the hash code. On the other hand, the best thing to do is to change a class’s toString() method to give a more human-friendly string representation. 

This is helpful when debugging, logging, or doing any other task that needs a string representation of an object. These bits of code show how to change the default implementation of a class’s toString() method to return a string that accurately describes an object. To learn more, visit Education Nest.

Press ESC to close