Home »
Java programming language
Java StringBuilder toString() method with example
StringBuilder Class toString() method: Here, we are going to learn about the toString() method of StringBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 22, 2019
StringBuilder Class toString() method
- toString() method is available in java.lang package.
- toString() method is used to represent string denoted by this object (when we create a new string object so first it is created and instantiated to contain the data[set of characters] denoted by this object currently).
- toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- toString() method does not throw an exception at the time of string representation.
Syntax:
public String toString();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is String, it returns the string denotation of this set of characters represented by this object.
Example:
// Java program to demonstrate the example
// of String toString() method of StringBuilder
public class ToString {
public static void main(String[] args) {
// Creating an StringBuilder object
StringBuilder st_b = new StringBuilder("Java ");
// By using append() method is to append the given string to
// st_b object
st_b.append("World");
// By using toString() method is to represent st_b
// object to String
System.out.println("st_b.toString() = " + st_b.toString());
}
}
Output
st_b.toString() = Java World