Introduction Java StringBuffer class

Objects of StringBuffer represents mutable strings. Each StringBuffer object creates an array of default or specified capacity that is used to hold the contents of StringBuffer.StringBuffer can be changed dynamically. Default capacity of StringBuffer object is 16 characters.


StringBuffer objects can be created using following constructors:-

  • StringBuffer(): creates an empty string buffer with the initial capacity of 16.
  • StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.
  • StringBuffer(String str): creates a string buffer with the specified string.

Commonly used methods of StringBuffer class:

  1. public synchronized StringBuffer append(String s): is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
  2. public synchronized StringBuffer insert(int offset, String s): is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
  3. public synchronized StringBuffer replace(int startIndex, int endIndex, String str): is used to replace the string from specified startIndex and endIndex.
  4. public synchronized StringBuffer delete(int startIndex, int endIndex): is used to delete the string from specified startIndex and endIndex.
  5. public synchronized StringBuffer reverse(): is used to reverse the string.
  6. public int capacity(): is used to return the current capacity.
  7. public void ensureCapacity(int minimumCapacity): is used to ensure the capacity at least equal to the given minimum.
  8. public char charAt(int index): is used to return the character at the specified position.
  9. public int length(): is used to return the length of the string i.e. total number of characters.
  10. public String substring(int beginIndex): is used to return the substring from the specified beginIndex.
  11. public String substring(int beginIndex, int endIndex): is used to return the substring from the specified beginIndex and endIndex.
  public class StringBufferDemo {

	public static void main(String[] args) {
		// Examples of Creation of Strings
		StringBuffer strBuf1 = new StringBuffer("Bobby");
		StringBuffer strBuf2 = new StringBuffer(100); // With capacity 100
		StringBuffer strBuf3 = new StringBuffer(); // Default Capacity 16
		System.out.println("strBuf1 : " + strBuf1);
		System.out.println("strBuf1 capacity : " + strBuf1.capacity());
		System.out.println("strBuf2 capacity : " + strBuf2.capacity());
		System.out.println("strBuf3 capacity : " + strBuf3.capacity());
		System.out.println("strBuf1 length : " + strBuf1.length());
		System.out.println("strBuf1 charAt 2 : " + strBuf1.charAt(2));
		// A StringIndexOutOfBoundsException is thrown if the index is not
		// valid.
		strBuf1.setCharAt(1, 't');
		System.out.println("strBuf1 after setCharAt 1 to t is : " + strBuf1);
		System.out.println("strBuf1 toString() is : " + strBuf1.toString());
		strBuf3.append("beginner-java-tutorial");
		System.out.println("strBuf3 when appended with a String : "
				+ strBuf3.toString());
		strBuf3.insert(1, 'c');
		System.out.println("strBuf3 when c is inserted at 1 : "
				+ strBuf3.toString());
		strBuf3.delete(1, 'c');
		System.out.println("strBuf3 when c is deleted at 1 : "
				+ strBuf3.toString());
		strBuf3.reverse();
		System.out.println("Reversed strBuf3 : " + strBuf3);
		strBuf2.setLength(5);
		strBuf2.append("jdbc-tutorial");
		System.out.println("strBuf2 : " + strBuf2);
		// We can clear a StringBuffer using the following line
		strBuf2.setLength(0);
		System.out.println("strBuf2 when cleared using setLength(0): "
				+ strBuf2);

	}
}
		

append ( ) method is used to append a primitive value or contents of a string to the StringBuffer.The append() method concatenates the given argument with this string.

  • public StringBuffer append(int i);
  • public StringBuffer append(char c);
  • public StringBuffer append(String s);
			 
class Test {

	public static void main(String args[]) {
		StringBuffer sb = new StringBuffer("Hello ");
		sb.append("Java");// now original string is changed
		System.out.println(sb);// prints Hello Java

	}

}

insert( )The insert() method is used to insert a string at the given position in the StringBuffer object.

public StringBuffer insert(int index, String s);

				 
class Test {

	public static void main(String args[]) {
		StringBuffer sb = new StringBuffer("Hello ");
		sb.insert(1, "Java");// now original string is changed

		System.out.println(sb);// prints HJavaello
	}
}
		

replace( ) method is used to replace part of a StringBuffer object with the given string.

public StringBuffer replace(int startIndex, int endIndex, String s);

class Test {

	public static void main(String args[]) {

		StringBuffer sb = new StringBuffer("Hello");
		sb.replace(1, 3, "NewDelhi");
		System.out.println(sb);
	}
}
	

reverse ( ) method reverses the contents of StringBuffer.

public StringBuffer reverse();

toString( ) method returns the content of StringBuffer as an immutable string.

public StringBuffer toString();