What is StringBuffer in Java?

In Java, the StringBuffer class is used to create a mutable string. A StringBuffer is like a String but it can be modified. Every StringBuffer has a capacity.

A StringBuffer contains some particular sequence of characters, but the length and the content of the sequence can be changed through certain methods.

In Strings the length and the memory location are final whereas StringBuffer has existing methods to modify these properties.

StringBuffer is safe for use by multiple threads. StringBuffer can be used to append, reverse, replace, concatenate and manipulate strings or sequence of characters.

StringBuffer can have characters and substrings inserted in middle or appended to the end. It will automatically grow to make room for such additions and has more characters preallocated than actually required.

Constructors of StringBuffer class

Some of the important constructors of the StringBuffer class are:

StringBuffer()

This creates an empty string buffer with an initial capacity of 16 characters.

The syntax is

StringBuffer s = new StringBuffer();
StringBuffer(String str)

This creates a string buffer with the specified string.

The syntax is

StringBuffer s = new StringBuffer("Java");
StringBuffer(int size)

This creates an empty string buffer with the specified size as length.

The syntax is

StringBuffer s = new StringBuffer(25);
StringBuffer(CharSequence seq)

This creates a string buffer that contains the same characters as specified by CharSequence.

The syntax is

CharSequence seq = "World";
StringBuffer s = new StringBuffer(seq);

Methods of StringBuffer class

Some of the most common methods of the StringBuffer class are:

length() and capacity()

The length() method returns the length of the StringBuffer.

The capacity() method returns the current capacity of the StringBuffer. The default capacity of the buffer is 16.

StringBuffer s = new StringBuffer("Programming");
System.out.println("Length of stringbuffer is: " + s.length());
System.out.println("Capacity of the stringbuffer is: " + s.capacity());

The output is

Length of stringbuffer is: 11
Capacity of the stringbuffer is: 27
append()

The append() method is used to add text to the end of the existing StringBuffer.

StringBuffer s = new StringBuffer("Java ");
s.append("Programmer"); // appends Programmer to the StringBuffer s
System.out.println(s);

The output is

Java Programmer
insert()

The insert() method inserts the given string to the StringBuffer at the specified position.

StringBuffer sb = new StringBuffer("Hello ");
sb.insert(6, "World");
System.out.println(sb);

The output is

Hello World
reverse()

The reverse() method reverses the characters of the StringBuffer and returns it.

StringBuffer s = new StringBuffer("Practice");
s.reverse();
System.out.println(s);

The output is

ecitcarP
replace()

The replace() method replaces the StringBuffer object with the specified string. It accepts three parameters startIndex, endIndex and the string. The substring that is to be replaced is specified by the startIndex and endIndex.

StringBuffer sb = new StringBuffer("The blue sky");
sb.replace(4, 8, "high");
System.out.println(sb);

The output is

The high sky
delete()

The delete() method deletes the character sequence in the StringBuffer. It accepts two parameters startIndex and endIndex. The characters are deleted from the startIndex until endIndex-1.

StringBuffer s = new StringBuffer("Joseph Roy");
s.delete(7, 10);
System.out.println(s);

The output is

Joseph
deleteCharAt()

The deleteCharAt() method deletes the character in the StringBuffer at the specified index.

StringBuffer s = new StringBuffer("A Programmer");
s.deleteCharAt(0);
System.out.println(s);

The output is

Programmer
ensureCapacity()

The ensureCapacity() method ensures that the StringBuffer capacity is at least equal to the specified minimum. If the current capacity is less than the specified capacity, then the current capacity is multiplied by 2 and 2 is added.((oldcapacity*2) + 2)

StringBuffer s = new StringBuffer("Multidimensional");
System.out.println(s.capacity()); // default(16)+16(capacity of s)
s.ensureCapacity(30);
System.out.println(s.capacity()); // no change since current capacity is 32
s.ensureCapacity(50);
System.out.println(s.capacity()); // new capacity = (32*2)+2 

The output is

32
32
66
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions