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.
String buffer has various different methods to modify the contents of the object
1. StringBuffer append(x): x may be int, float, double, char, String, or StringBuffer. It will be appended to the calling StringBuffer.
2. StringBuffer insert(int offset, x): may be int, float, double, char, String, or StringBuffer. It will be inserted into the calling StringBuffer at offset.
3. StringBuffer delete(int start, int end): Removes the characters from start to end.
4. StringBuffer reverse(): reverses the character sequence in the StringBuffer.
5. String toString(): converts StringBuffer into a String.
6. int length(): Returns the length of the StringBuffer.
* A demo class to explain about String object
* @author Santhosh Reddy Mandadi
* @since 02-Mar-2012
* @version 1.0
public class StringBufferDemo
public static void main(String args) throws Exception
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sur = br.readLine();
System.out.println("Enter middle name:");
String mid = br.readLine();
System.out.println("Enter last name:");
String last = br.readLine();
//Creating a new string buffer object
StringBuffer sb = new StringBuffer();
//Appending sur, last, to sb
System.out.println("Full name: "+sb);
Enter middle name:
Enter last name:
Full name: MandadiSanthoshReddy
Above program has been tested in 1.5 and 1.6. Click here to check out other programs that I've developed.