StringBuffer class in Java

Why use StringBuffer?

In Java, the StringBuffer class is used to create mutable (meaning modifiable) sequence of characters. You already have studied in previous classes that the String class is an immutable type. This means that once a String object is created, it cannot be modified. To bring about changes, we must bring those changes in a new, separate object. However, that’s not the case with the StringBuffer class. A StringBuffer object is mutable, and so it is possible to modify the original object.

StringBuffer is More Efficient in Certain Operations

The StringBuffer class is more efficient in certain operations that involve frequent changes to strings, such as appending, inserting, or deleting characters.

StringBuffer has Obviously, a Buffer

StringBuffer has an initial capacity, that can automatically expand as needed when more characters are added. This capacity acts as a buffer. To access the value of this buffer, you may call the capacity() method. The capacity of an empty StringBuffer object is 16.

StringBuffer sb = new StringBuffer("Hello");
System.out.println("Capacity = " + sb.capacity());

More useful methods…

The length() method returns the length of the string, which is the total number of characters currently stored in the string.

StringBuffer sb = new StringBuffer("Hello");
System.out.println("Length = " + sb.length());

The toString() method can be used to convert a StringBuffer object to a String object.

StringBuffer sb = new StringBuffer("Hello");
String s = sb.toString();
System.out.println(s);

The append() method is used to add a substring or a character at the end of the existing StringBuffer object.

StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
sb.append('!');
System.out.println(sb);

The insert() method is used to insert a character or a string at a given index of an existing StringBuffer object.

StringBuffer sb = new StringBuffer("Tech");
sb.insert(2, 'a');
sb.insert(5, "er");
System.out.println(sb);

In the above code snippet, firstly we are inserting a character ‘a’ at index 2. As a result, the StringBuffer object becomes “Teach”. Later, we are inserting a string “er” at index 5, so the StringBuffer object becomes “Teacher”.

The delete() method lets us delete substrings from an existing StringBuffer object.

StringBuffer sb = new StringBuffer("Teach");
sb.delete(2, 3);
System.out.println(sb);

In the above code snippet, the substring “a” is deleted, because the deletion starts at index 2 and stops at index 3.

The replace() method is used to replace one substring with another.

StringBuffer sb = new StringBuffer("Teacher");
sb.replace(0, 1, "Pr");
System.out.println(sb);

The reverse() method reverses the string.

StringBuffer sb = new StringBuffer("won");
sb.replace();
System.out.println(sb);

The setCharAt() method sets the character of the string at the specified index.

StringBuffer sb = new StringBuffer("now");
sb.setCharAt(1, 'e');
System.out.println(sb);

Leave a Reply

Your email address will not be published. Required fields are marked *