String toString() is the built-in method of java.lang which return itself a string. So here no actual conversion is performed. Since toString() method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly.
Syntax :
public String toString()
Parameter: The method does not accept any parameters .
Return Value: This method returns the string itself.
Examples :
Input: String("geeksforgeeks")
Output: geeksforgeeks
Below programs illustrate the Java.lang.String.toString() method:
Program 1:
import java.io.*;
public class Geeks {
public static void main(String args[])
{
String Strobj = new String( "Welcome to the world of geeks." );
System.out.print( "Output String Value :" );
System.out.println(Strobj.toString());
String Strobj2 = new String( "Let's make it simple for you." );
System.out.print( "Output String Value :" );
System.out.println(Strobj2.toString());
}
}
|
Output:
Output String Value :Welcome to the world of geeks.
Output String Value :Let's make it simple for you.
Program 2:
import java.io.*;
public class Geeks {
public static void main(String args[])
{
String Strobj = new String( "THank You" );
System.out.print( "Output : " );
System.out.println(Strobj.toString());
}
}
|
Output:
Output : THank You
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Jul, 2018
Like Article
Save Article