This

StringBuffer sb = new StringBuffer("Welcome");
String st = sb + "";

will result more or less in

StringBuffer sb = new StringBuffer("Welcome");
StringBuilder builder = new StringBuilder();
builder.append((sb == null) ? "null" : sb.toString());
builder.append("");
String st = builder.toString();
Answer from Sotirios Delimanolis on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2015 › 04 › convert-stringbuffer-to-string
Java – How to convert StringBuffer to String
The toString() method of StringBuffer class can be used to convert StringBuffer content to a String. This method returns a String object that represents the contents of StringBuffer.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-tostring-method-in-java-with-examples
StringBuffer toString() method in Java with Examples - GeeksforGeeks
December 4, 2018 - The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer ...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuffer_tostring.htm
Java StringBuffer toString() Method
The Java StringBuffer toString() method is used to retrieve a string representing the data in a StringBuffer object. A new String object is allocated and initialized to contain the character sequence currently represented by this object.
🌐
Coderanch
coderanch.com › t › 397906 › java › Cast-StringBuffer-String
Cast from StringBuffer to String (Beginning Java forum at Coderanch)
You will want to use the append() method of StringBuffer when adding to string data in a StringBuffer object. Then, call StringBuffer's toString() method to return the entire contents as a string value.
Top answer
1 of 2
9

This

StringBuffer sb = new StringBuffer("Welcome");
String st = sb + "";

will result more or less in

StringBuffer sb = new StringBuffer("Welcome");
StringBuilder builder = new StringBuilder();
builder.append((sb == null) ? "null" : sb.toString());
builder.append("");
String st = builder.toString();
2 of 2
6

If we compile them into two methods in java

private StringBuffer sb = new StringBuffer("Welcome");

public String testToString() {
  return sb.toString();
}

public String testAppend() {
  return sb + "";
}

and then use javap -v, we get

public java.lang.String testToString();
  descriptor: ()Ljava/lang/String;
  flags: ACC_PUBLIC
  Code:
    stack=1, locals=1, args_size=1
       0: aload_0       
       1: getfield      #19                 // Field sb:Ljava/lang/StringBuffer;
       4: invokevirtual #27                 // Method java/lang/StringBuffer.toString:()Ljava/lang/String;
       7: areturn       
    LineNumberTable:
      line 10: 0
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0       8     0  this   Lcom/stackoverflow/Question;

public java.lang.String testAppend();
  descriptor: ()Ljava/lang/String;
  flags: ACC_PUBLIC
  Code:
    stack=2, locals=1, args_size=1
       0: new           #31                 // class java/lang/StringBuilder
       3: dup           
       4: invokespecial #33                 // Method java/lang/StringBuilder."<init>":()V
       7: aload_0       
       8: getfield      #19                 // Field sb:Ljava/lang/StringBuffer;
      11: invokevirtual #34                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      14: invokevirtual #38                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      17: areturn       
    LineNumberTable:
      line 14: 0
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      18     0  this   Lcom/stackoverflow/Question;

So appending appears to be slightly less efficient as it uses an extra bit of stack and contains more instructions.

🌐
Coderanch
coderanch.com › t › 385588 › java › convert-StringBuffer-String
How to convert StringBuffer to String ? (Java in General forum at Coderanch)
Try the following code: StringBuffer sbuf = new StringBuffer("sameer"); String str = sbuf.toString(); System.out.println(str); toString() will convert from StringBuffer to String object
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuffer-tostring
Java StringBuffer toString()
public string toString(): Returns a String representation of the StringBuffer sequence.
Find elsewhere
🌐
University of Texas
cs.utexas.edu › ~mitra › csSummer2012 › cs312 › lectures › strings.html
String and StringBuffer
Converting Numeric values to Strings · double x = 2.512; String xStr = String.valueOf ( x ); Test Prefix and Suffix of Strings · String str = "polymorphism"; str.startsWith ("poly") => true str.endsWith ("ism") => true · Splitting a String · String line = "This is a test"; String[] result = line.split ( "\\s" ); for ( int i = 0; i < result.length; i++ ) System.out.println ( result[i] ); A String object is immutable. A StringBuffer object is like a String object but can be modified.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-class-in-java
StringBuffer Class in Java - GeeksforGeeks
StringBuffer(String str): It accepts a string argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.
Published: May 27, 2026
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuffer.html
StringBuffer (Java Platform SE 8 )
July 21, 2026 - String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type.
🌐
SAP Community
community.sap.com › t5 › technology-q-a › convert-stringbuffer-to-string › qaq-p › 2119121
convert StringBuffer to String[] - SAP Community
April 10, 2007 - To convert a String buffer into a String we have toString() method. for example : x = "a" + 4 + "c" is similar to · x = new StringBuffer().append("a").append(4).append("c") .toString() where x is a string..
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuffer.html
StringBuffer (Java Platform SE 7 )
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type.
🌐
MIT
web.mit.edu › java_v1.0.2 › www › javadoc › java.lang.StringBuffer.html
Class java.lang.StringBuffer
Converts to a String representing the data in the buffer. ... Constructs an empty String buffer. StringBuffer
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-to-convert-stringbuffer-to-string-7073
How To Convert StringBuffer to String - Oracle Forums
April 25, 2013 - hi i want to convert the StringBuffer in to String i have used this code but geives compile error. import java.io.*; import java.util.*; import javax.mail.*; import java.sql.*; public class REPLACE ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_buffer.htm
String Buffer and String Builder Classes
The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters. Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without