StringBuffer (Java)

Send Us a Sign! (Contact Us!)

StringBuffer is a mutable sequence of characters. It is like a String but the contents of the StringBuffer can be modified after creation.

StringBuffer Capacity

Every StringBuffer object has a capacity associated with it. The capacity of the StringBuffer is the number of characters it can hold. It increases automatically as more contents added to it.

StringBuffer’s current capacity can be obtained using following method.

int capacity()

This method returns the current capacity of the StringBuffer object. For example:

StringBuffer stringBuffer = new StringBuffer(“Hello World”);
System.out.println(stringBuffer.capacity());

This will print 27 (11 + 16) on console when executed. The actual number of characters in StringBuffer can be obtained by following method.

int length()

Returns the actual number of characters contained in the StringBuffer object. For example:

StringBuffer stringBuffer = new StringBuffer(“Hello World”);
System.out.println(stringBuffer.length());
System.out.println(stringBuffer.capacity());

This will print on console when run:

11
27

Specifying initial capacity of StringBuffer

Initial capacity of the StringBuffer object can be specified using following method.

void ensureCapacity(int initialCapacity)

Ensures that the StringBuffer’s initial capacity would be grater than or equal to the specified initial capacity. The new capacity of the StringBuffer is the maximum of:

  1. The initialCapacity argument passed.
  2. ( Old capacity * 2 ) + 2.

StringBuffer Constructors

StringBuffer object can be created using following StringBuffer constructors.

StringBuffer()

Creates empty StringBuffer object having initial capacity of 16.

For example:

StringBuffer stringBuffer = new StringBuffer();

Here, capacity of stringBuffer object would be 16.

StringBuffer(int capacity)

Creates empty StringBuffer object having initial capacity specified.

For example:

StringBuffer stringBuffer = new StringBuffer(50);

Here, capacity of stringBuffer object would be 50.

This constructor may throw NegativeArraySizeException if the passed argument is less than 0.

StringBuffer(String content)

Creates new StringBuffer object having contents same as the argument string object. The initial capacity of the newly created StringBuffer object will be the length of the argument string object + 16.

For example:

String str = “Hello”;
StringBuffer stringBuffer = new StringBuffer(str);

Here, capacity of stringBuffer object would be 5 + 16 = 21.

String concatenation and StringBuffer

In Java, String concatenation operator (+) is internally implemented using StringBuffer.

For example:

String str = “Hello” + “World”;

Would be executed as:

String str = new StringBuffer().append(“Hello”).append(“World”).toString();

First an empty StringBuffer will be created and then operands of the + operator would be appended using append method of the StringBuffer. Finally toString() method of the StringBuffer would be called to convert it to string object and the same will be returned.

String concatenation using this approach is very expensive in nature, because it involves creation of temporary StringBuffer object. Then that temporary object’s append method gets called and the resulting StringBuffer would be converted back to String using toString() method.

When to use String and when StringBuffer?

If there is a need to change the contents frequently, StringBuffer should be used instead of String because StringBuffer concatenation is significantly faster than String concatenation.

Java StringBuffer Reverse Example

/*
   Java StringBuffer Reverse Example
   This example shows how to reverse the content of the StringBuffer using
   reverse method of Java StringBuffer class.
*/
 
public class JavaStringBufferReverseExample {
 
  public static void main(String[] args) {
   
    //create StringBuffer object
    StringBuffer sb = new StringBuffer("Java StringBuffer Reverse Example");
    System.out.println("Original StringBuffer Content : " + sb);
   
    //To reverse the content of the StringBuffer use reverse method
    sb.reverse();
    System.out.println("Reversed StringBuffer Content : " + sb);
  }
}
 
/*
Output Would be
Original StringBuffer Content : Java StringBuffer Reverse Example
Reversed StringBuffer Content : elpmaxE esreveR reffuBgnirtS avaJ
*/