StringBuffer And StringBuilder Class

As we know that strings are immutable and cannot be modified, to overcome this we got another classes called 'StringBuilder' and 'StringBuffer' which represents strings in such a way that their data can be modified. Their class objects are mutable, and there are methods provided in this class which directly manipulate the data inside the object.

Creating StringBuffer Objects

There are two ways to create a StringBuffer object, and fill the object with a string

  • We can create a StringBuffer object by using the new operator and pass the string to the object as follows
StringBuffer sb = new StringBuffer("Hello"); // we are passing the string "Hello" to the StringBuffer object 'sb'
System.out.println(sb); // this will give output as "Hello"
  • Another way of creating a StringBuffer object is to first allotting memory to the StringBuffer object using new operator and later storing the string into it, as
StringBuffer sb = new StringBuffer(); // allotting memory using new operator
StringBuffer sb = new StringBuffer(50); //storing string 

StringBuffer Class Methods

  • StringBuffer append(x)
  • StringBuffer insert(int i, x)
  • StringBuffer delete(int i, int j)
  • StringBuffer reverse()
  • String toString()
  • int length()
  • int indexOf(String str)
  • StringBuffer replace(int i, int j, String str)
  • String substring(int i)

StringBuffer append(x)

Here x may be boolean, byte, int, long, float, double, char, string or another StringBuffer. It will be added to the StringBuffer object.

  • Example
StringBuffer sb = new StringBuffer("uni");
sb.append("versity"); // the output will be "university"

StringBuffer insert(int i, x)

Here x may be boolean, byte, int, long, float, double, char, string or another StringBuffer. It will be inserted into the StringBuffer at the position represented by ‘i’.

  • Example
StringBuffer sb = new StringBuffer("Intelligent Person");
sb.insert(11, "young"); // the output will be "Intelligentyoung Person"

Here, the stringBuffer object sb contains “Intelligent Person”, sb.insert(11,“young”) will insert the string, “young” at 11th position and hence we will get the above mentioned output.

StringBuffer delete(int i, int j)

This removes the characters from ith position till j-1st position in the StringBuffer.

  • Example
StringBuffer sb = new StringBuffer("University"); // deletes the characters from the beginning to 2nd character("uni") and the resultant string in `sb` will be "versity".

StringBuffer reverse()

This reverses the characters sequence in the StringBuffer.

  • Example - If the StringBuffer contains “abc”, it becomes “cba”.

String toString()

This converts the StringBuffer object into a string object. This will enable us to use String class methods on StringBuffer object, after its conversion.

int length()

This returns the number of characters in the StringBuffer object.

int indexOf(String str)

This returns the first occurrence of substring ‘str’ in the StringBuffer object.

  • Example
s1 = "This is a book";
s2 = "is"
int n = s1.indexOf(s2); 
/* this will search for substring "is" in the main string "This is a book" and 
returns the output as `2` as "is" can be seen at the 2nd position. We can 
observe that "is" is present at 2nd and 5th positions but it will return only 
the first ocurring one.*/

StringBuffer replace(int i, int j, String str)

This replaces the characters from i to j-1, by the string str in the StringBuffer object.

  • Example
StringBuffer sb = new StringBuffer("High Cost");
sb.replace(0,4, "Low"); // we are replacing first 4 characters of `sb` by the 3 characters, "Low". Now `sb` contains, "Low Cost".

String substring(int i)

This extracts a sub string from the StringBuffer object starting from ith position till the end.

  • Example
StringBuffer sb = new StringBuffer("New Delhi");
String s = sb.substring(4); // preceding statement retrieves characters from 4th position till the end, and hence returns "Delhi" into s.

StringBuilder Class

StringBuilder class has been added in jdk 1.5 which has same features like StringBuffer class. StringBuilder class objects are also mutable as are the StringBuffer objects.

StringBuilder Class Methods

  • StringBuilder append(x)
  • StringBuilder insert(int i, x)
  • StringBuilder delete(int i, int j)
  • StringBuilder reverse()
  • String toString()
  • int length()
  • int indexOf(String str)
  • StringBuilder replace(int i, int j, String str)
  • String substring(int i)

The main difference between StringBuilder and StringBuffer classes is that the StringBuffer class is synchronized by default and StringBuilder class is not. This means, when several threads process or act on a StringBuffer class object, they are executed one by one on the object, thus ensuring reliable results. But in case of StringBuilder object, since it is not synchronized, it allows several threads to act on it simultaneously, which may lead to inaccurate results.

Subscribe For More Content