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.
🌐
Coderanch
coderanch.com › t › 388160 › java › Converting-String-StringBuffer
Converting from a String to a StringBuffer (Beginning Java forum at Coderanch)
programming forums Java Mobile ... forums · this forum made possible by our volunteer staff, including ... ... Just pass on the Stringbuffer object into String and create a new object....
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 class JavaExample { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); //char array char[] ch = {'h','e','l','l','o'}; //append array to StringBuffer sb.append(ch); //convert StringBuffer to String String str = sb.toString(); System.out.println("String: "+str); } }
Find elsewhere
🌐
University of Texas
cs.utexas.edu › ~mitra › csSummer2012 › cs312 › lectures › strings.html
String and StringBuffer
The principal operations on a StringBuffer are append() and insert() methods. The append() method adds characters at the end of the buffer and the insert() method adds the characters at a specified location. Every string buffer has a capacity. As long as the length of the character sequence ...
🌐
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
public class Test { public static void main(String args[]) { StringBuffer sBuffer = new StringBuffer("test"); sBuffer.append(" String Buffer"); System.out.println(sBuffer); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-vs-stringbuilder-vs-stringbuffer-in-java
String vs StringBuilder vs StringBuffer in Java - GeeksforGeeks
April 23, 2026 - If a string is going to remain constant throughout the program, then use the String class object because a String object is immutable. If a string can change (for example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough. If a string can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous, so you have thread-safety.