StringBuilder toString() method in Java with Examples
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.
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);
String contains = GeeksForGeeks
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.
Single string generated using toString() method is --> Are You A Programmer