A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

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.


StringBufferDemo.java
/**
* A demo class to explain about String object
* @author Santhosh Reddy Mandadi
* @since 02-Mar-2012
* @version 1.0
*/

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StringBufferDemo
{
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter surname:");
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
sb.append(sur);
sb.append(last);
System.out.println(sb);
sb.insert(sur.length(), mid);
System.out.println("Full name: "+sb);
//In reverse
System.out.println("Reversed: "+sb.reverse());
}
}


Output:
Enter surname:
Mandadi
Enter middle name:
Santhosh
Enter last name:
Reddy
MandadiReddy
Full name: MandadiSanthoshReddy
Reversed: yddeRhsohtnaSidadnaM


Above program has been tested in 1.5 and 1.6. Click here to check out other programs that I've developed.