Java toString String Method

The Java toString Method is useful to return the string (It is also an Object) representation of the user-specified Object. This article will show how to use this toString method with an example.

The syntax of the toString is as shown below. The following Java toString method will not accept any parameters and convert the Object into a string.

public String toString(); 

//In order to use in program
Object.toString()

Java toString String Method Example

This function returns the string representation of the object specified by the user. This program will help to understand this method.

In the str4 statement, first, it will convert the Object into a String. The following Java System.out.println statements will print the output. Within the last statement, we used this method on the string object str4. From the above screenshot, see that it returns the string representation of the sentence.

public class Example {	
	
	public static void main(String[] args) 
	{
		 String str = "Learn Programming at Tutorial GateWay";
		 
		 String str1 = str.toString();
		 String str2 = "Programming Language".toString();
		 String str3 = "Tutorial Gateway".toString();
		 String str4 = new String("Read This Programming Sentence");
		 
		 System.out.println( "Result = " + str1);
		 System.out.println( "Result = " + str2);
		 System.out.println( "Result = " + str3);
		 System.out.println( "Result = " + str4.toString());
	}
}
Java String toString method Example