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.


    public String toString();


  • 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.


// 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


        // By using toString() method is to represent st_b
        // object to String
        System.out.println("st_b.toString() = " + st_b.toString());


st_b.toString() = Java World


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.