String toString() Method in java with Examples
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:
// Java program to illustrate the // Java.lang.String.toString() method 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 String Value :Welcome to the world of geeks. Output String Value :Let's make it simple for you.
Program 2:
// Java program to illustrate the // Java.lang.String.toString() method 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 : THank You
Recommended Posts:
- Calendar toString() Method in Java with Examples
- Duration toString() method in Java with Examples
- Modifiers toString() method in Java with Examples
- DigestOutputStream.toString() method in Java with Examples
- Byte toString() method in Java with examples
- Currency toString() Method in Java with Examples
- Provider toString() method in Java with Examples
- OptionalInt toString() method in Java with examples
- ByteBuffer toString() method in Java with Examples
- MonthDay toString() Method in Java with Examples
- OptionalLong toString() method in Java with examples
- OptionalDouble toString() method in Java with examples
- OffsetTime toString() method in Java with examples
- ChronoPeriod toString() method in Java with Examples
- Writer toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



