The StringBuffer
class in Java is used to create mutable (modifiable) string objects. It is similar to the StringBuilder
class but is synchronized, meaning it is thread-safe.
The StringBuffer
class is part of the java.lang
package. Unlike String
, which is immutable, instances of StringBuffer
can be modified after they are created. This makes StringBuffer
more efficient for operations that involve frequent modifications to the string.
Creating StringBuffer
Objects
StringBuffer buffer1 = new StringBuffer(); // Default capacity 16
StringBuffer buffer2 = new StringBuffer(50); // Custom capacity
StringBuffer buffer3 = new StringBuffer("Hello"); // Initialized with a string
Capacity
Capacity refers to the total number of character storage size in a string buffer. When you create a StringBuffer
object, it allocates a certain amount of memory to store the string content. This memory allocation is called the capacity. If the number of characters added to the StringBuffer
exceeds its current capacity, the capacity is automatically increased to accommodate the additional characters.
When a StringBuffer
is created using the default constructor, it has an initial capacity of 16 characters. If you provide an initial string, the capacity is set to 16 plus the length of the provided string.
By preallocating memory for a certain number of characters (the capacity), the StringBuffer
class avoids frequent reallocation of memory as characters are appended or inserted. This can significantly improve performance, especially when working with large strings or performing extensive string manipulations.
If the number of characters in the StringBuffer
exceeds its capacity, the capacity is automatically increased. The new capacity is calculated as (old capacity * 2) + 2
. This mechanism helps in reducing the number of memory reallocations needed when adding a large number of characters to the StringBuffer
.
The capacity differs from the length of a string as the length only calculates the number of characters present in the string, while capacity calculates the maximum number of characters the StringBuffer can fit at the moment.
StringBuffer buff = new StringBuffer("TutorialsPoint");
// returns the current capacity of the String buffer i.e. 16 + 14
System.out.println("capacity = " + buff.capacity());
buff = new StringBuffer(" ");
// returns the current capacity of the String buffer i.e. 16 + 1
System.out.println("capacity = " + buff.capacity());
Common Methods
append()
: Appends the specified string representation to the end of the StringBuffer
.
StringBuffer buffer = new StringBuffer("Hello");
buffer.append(", World!");
System.out.println(buffer.toString()); // Output: "Hello, World!"
insert()
: Inserts the specified string into the StringBuffer
at the specified position.
StringBuffer buffer = new StringBuffer("Hello");
buffer.insert(5, ", World");
System.out.println(buffer.toString()); // Output: "Hello, World"
delete()
: Removes characters from the StringBuffer
based on the specified range.
StringBuffer buffer = new StringBuffer("Hello, World!");
buffer.delete(5, 12);
System.out.println(buffer.toString()); // Output: "Hello!"
deleteCharAt()
: Removes the character at the specified position from the StringBuffer
.
StringBuffer buffer = new StringBuffer("Hello, World!");
buffer.deleteCharAt(5);
System.out.println(buffer.toString()); // Output: "Hello World!"
replace()
: Replaces the characters in the StringBuffer
with characters from the specified string.
StringBuffer buffer = new StringBuffer("Hello, World!");
buffer.replace(7, 12, "Java");
System.out.println(buffer.toString()); // Output: "Hello, Java!"
reverse()
: Reverses the characters in the StringBuffer
.
StringBuffer buffer = new StringBuffer("Hello");
buffer.reverse();
System.out.println(buffer.toString()); // Output: "olleH"