Here we will learn how to convert StringBuffer to String using toString method of String class in Java.
Source Code
package com.beginner.examples;
public class StringBufferToString {
public static void main(String[] args) {
//create StringBuffer.
StringBuffer stringBuffer = new StringBuffer("Hello World!");
/*
* Use toString() method of StringBuffer to convert StringBuffer to String.
*/
String string = stringBuffer.toString();
System.out.println("StringBuffer to String: " + string);
}
}
Output:
StringBuffer to String: Hello World!