Open In App

StringBuilder toString() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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 classes
import java.io.*;
 
// Main class
class 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());
    }
}


Output

String contains = GeeksForGeeks

Example 2:

Java




// Java program to demonstrate toString() Method.
 
// Importing input output classes
import java.io.*;
 
// Main class
class 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());
    }
}


Output

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 classes
import java.io.*;
import java.util.*;
 
// Main class
class 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);
    }
}


Output

Single string generated using toString() method is --> Are You A Programmer


Last Updated : 23 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads