Skip to Content

StringBuffer toString Java Example

  1. /*
  2.         StringBuffer toString Java Example
  3.         This example shows how to convert StringBuffer to String in Java using
  4.         toString method of String class.
  5. */
  6.  
  7. public class JavaStringBufferToStringExample {
  8.  
  9.         public static void main(String args[]){
  10.                
  11.                 //create StringBuffer object
  12.                 StringBuffer sbf = new StringBuffer("Hello World!");
  13.                
  14.                 /*
  15.                  * To convert StringBuffer to String object, use
  16.                  * String toString() method of StringBuffer class.
  17.                  */
  18.                
  19.                 String str = sbf.toString();
  20.                
  21.                 System.out.println("StringBuffer to String: " + str);
  22.         }
  23. }
  24.  
  25. /*
  26. Output of above given StringBuffer to String example would be
  27. StringBuffer to String: Hello World!
  28. */