StringBuilder toString() method in Java with Examples

30 Aug, 2021
The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString(). Subsequent changes to this sequence contained by Object do not affect the contents of the String.
Syntax: 
public abstract String toString() ;
Return Value: This method always returns a string representing the data contained by StringBuilder Object.
As this is a very basic method been incorporated in java so we will be discussing it within our clean java programs to get to know its working. INternally in the class, it is defined as follows which will give you a better understanding of how it actually works. 
return getClass().getName()+ "@" + Integer.toHexString(hashCode);
Example 1:
Java
// Java program to demonstrate toString() Method // Importing I/O classesimport java.io.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Creating a StringBuilder object        // with a String pass as parameter        StringBuilder str            = new StringBuilder("GeeksForGeeks");         // Print and display the string        // using standard toString() method        System.out.println("String contains = "                           + str.toString());    }}
String contains = GeeksForGeeks
Example 2:
Java
// Java program to demonstrate toString() Method. // Importing input output classesimport java.io.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Creating a StringBuilder object with        // a String pass as parameter        StringBuilder str = new StringBuilder(            "Geeks for Geeks contribute");         // Print and display the string        // using to.String() method        System.out.println("String contains = "                           + str.toString());    }}
String contains = Geeks for Geeks contribute
Now we are done with discussing basic examples let us operate the same over an array of strings by converting it to a single string using to.String() method. Here we will create a StringBuilder class object then we will store the array of string as its object. Later on, we will create another string and will throw all elements in this. Lastly, we will print this string.
Example 3:
Java
// Java Program to Convert Array of Strings to A String// Using toString() method // Importing required classesimport java.io.*;import java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Random array of string as input        String gfg[] = { "Are", "You", "A", "Programmer" };         // Creating object of StringBuilder class        StringBuilder obj = new StringBuilder();         // Adding above arrays of strings to        // Stringbuilder object        obj.append(gfg[0]);        obj.append(" " + gfg[1]);        obj.append(" " + gfg[2]);        obj.append(" " + gfg[3]);         // Note if elements are more then        // we will be using loops to append(add)         // Creating a single string        String str = obj.toString();         // Print and display the above string        // containing all strings as a single string        // using toString() method        System.out.println(            "Single string generated using toString() method is --> "            + str);    }}
Single string generated using toString() method is --> Are You A Programmer
Akanksha_Rai
solankimayank
arorakashish0911
surinderdawra388
java-basics
Java-Functions
Java-StringBuilder
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java