StringBuffer sb = new StringBuffer("Hello"); String str = sb.toString(); System.out.println(str);
StringBuffer sb1 = new StringBuffer(); sb1.append("Java"); sb1.append(" is"); sb1.append(" my"); sb1.append(" favourite"); sb1.append(" language"); String str1 = sb1.toString(); System.out.println(str1);In the above example, a new StringBuffer instance "sb1" is created with multiple appends. The sb1.toString() creates a new instance of a string "str1" with the same content as "sb1" and finally printed "str1". The StringBuffer class is a part of the java.lang package library.