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 toString() 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
