JavaScript

How to Use the toString() in Java

In Java programming, there can be a requirement to contain the data in an identical format. For instance, managing all the entries in the “string” format to apply the string operations upon them in one go. In such scenarios, the “toString()” method in Java assists the developer to utilize the data differently and improve the code readability.

This write-up will elaborate on the usage and implementation of the “toString()” method in Java.

What is “toString()” in Java?

The “toString()” method in Java returns the provided value in “string” format. This method can be applied to various data types and return the corresponding value in a string.

Syntax

String toString(int)

In this syntax:

  • int” points to the integer for which the string representation needs to be returned.

Example 1: Applying “toString()” Method Upon Multiple Data Types
In this example, the “toString()” method can be applied to return the string representation of multiple data types:

System.out.println("The converted double to string is: "+Double.toString(11.0));
System.out.println("The converted integer to string is: "+Integer.toString(12));
System.out.println("The converted long to string is: "+Long.toString(123213123));
System.out.println("The converted boolean to string is: "+Boolean.toString(false));

In the above lines of code, simply associate the “toString()” method with double, integer, long, and boolean data types, respectively, and return the “string” representation of these values.

Output

In this output, it can be seen that the string representation of each of the specified data types is returned.

Example 2: Overriding the “toString()” Method as a Class Function
In this particular example, the “toString()” method can be overridden to return the object values as “string”:

class ID{
 String city;
 Integer serial;
 ID(String c, Integer s){
  this.city= c;
  this.serial= s;
}
public String toString(){
 return "The city is " + this.city + " and the corresponding serial is " +   this.serial;
}}
public class toString2 {
 public static void main( String args[] ) {
  ID i = new ID("London", 5);
  System.out.println(i.toString());
}}

In this code block, apply the following steps:

  • Firstly, define a class named “ID” and specify the stated variables.
  • Now, include the class constructor having the provided parameters.
  • In the constructor definition, refer to the specified variables via “this” and assign them the parameter values.
  • After that, override the “toString()” method and return the passed constructor arguments as a “string” via its(method) definition.
  • In the “main” method, create an object of the class using the “new” keyword and the “ID()” constructor, respectively.
  • Pass the stated values as constructor arguments and return these values in the form of “string” via invoking the overridden “toString()” method with the help of the created object.

Output

In this outcome, it can be implied that the passed constructor arguments are returned as a “string”.

Conclusion

The “toString()” method in Java returns the provided value into a string representation and can be applied to multiple data types. This method can also be applied as a class function to return the string format of the passed values. This blog discussed the usage of the “toString()” method in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.