StringBuffer(String str) – Creates a string with an initial value contained by str. The total capacity of created string is the sum of length of initial value and 16 more characters.

StringBuffer(CharSequence chars) – Creates a string with an initial value contained by chars. It also reserves extra space for 16 more characters.

Note: When we do modification in a given string and if the length of new string exceeds the current capacity then the capacity is increased by (oldcapacity*2)+2.

Below I have discussed few important methods of StringBuffer class which are different from methods of String in Java.

Java StringBuffer Methods

StringBuffer length() and capacity() Methods

The length of string value present in StringBuffer object is calculated by length() method. The total capacity of an object is calculated by capacity() method. See below example code to understand it more clearly.

Syntax

1

2

intlength()

intcapacity()

Example

1

2

3

4

5

6

7

8

9

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("Hello");

System.out.println("length = "+str.length());

System.out.println("capacity = "+str.capacity());

}

}

Output

In above example str.length() gives output 5 because length of Hello is 5. While str.capacity() gives output 21 because space for 16 more characters is automatically added.

StringBuffer ensureCapacity() Method

This method ensures that the given capacity is the minimum to the current capacity. If the given capacity is more than the current capacity then the current capacity is increased by (oldcapacity*2)+2. For example if the current capacity is 20 then it will become (20*2)+2 = 42.

Syntax

1

voidensureCapacity(intminCapacity)

Example

1

2

3

4

5

6

7

8

9

10

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("Hello");

System.out.println("capacity = "+str.capacity());

str.ensureCapacity(30);

System.out.println("capacity = "+str.capacity());

}

}

Output

StringBuffer setLength() Method

As its name shows, it is used to set the length of string within an object of StringBuffer class. If the new length is less than the current length then the characters beyond the new length are lost.

Syntax

1

voidsetLength(intlen)

StringBuffer charAt() and setCharAt() Methods

The charAt() method is used to access a particular character in given string. It is same as we use for String class object. We can replace a particular character in a string with a new character using setCharAt() method.

Syntax

1

2

charcharAt(intwhere)

voidsetCharAt(intwhere,charch)

Example

1

2

3

4

5

6

7

8

9

10

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("Hello");

System.out.println("before = "+str);

str.setCharAt(1,'a');

System.out.println("after = "+str);

}

}

Output

StringBuffer append() Method

The append() method is used to add a string at the end of given string. It has several overloaded versions as given below.

Syntax

1

2

3

StringBuffer append(Stringstr)

StringBuffer append(intnum)

StringBuffer append(Objectobj)

Example

1

2

3

4

5

6

7

8

9

10

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("Hello");

System.out.println("before = "+str);

str.append("World");

System.out.println("after = "+str);

}

}

Output

StringBuffer insert() Method

The insert() method is used to insert a string in given string. Same as like append() method it also has various overloaded versions. We have to specify the index where we want to insert and the string to be inserted as arguments.

Syntax

1

2

3

StringBuffer insert(intindex,Stringstr)

StringBuffer insert(intindex,charch)

StringBuffer insert(intindex,Objectobj)

Example

1

2

3

4

5

6

7

8

9

10

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("TheProgrammer");

System.out.println("before = "+str);

str.insert(3,"Crazy");

System.out.println("after = "+str);

}

}

Output

StringBuffer reverse() Method

The characters within a StringBuffer object can be reversed using reverse() method.

Syntax

1

StringBuffer reverse()

Example

1

2

3

4

5

6

7

8

9

10

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("TheCrazyProgrammer");

System.out.println("before = "+str);

str.reverse();

System.out.println("after = "+str);

}

}

Output

StringBuffer replace() Method

The replace() method is used to replace a part of given string with another string. We have to just specify the startindex, endindex and the string with which we want to replace as arguments.

Note: Here the end index is one less than what we have specified in the argument i.e. endIndex – 1.

Syntax

1

StringBuffer replace(intstartIndex,intendIndex,Stringstr)

Example

1

2

3

4

5

6

7

8

9

10

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("TheCrazyProgrammer");

System.out.println("before = "+str);

str.replace(8,18,"Coder");

System.out.println("after = "+str);

}

}

Output

StringBuffer delete() and deleteCharAt() Method

The delete() method is used to delete a sequence of characters from start to end index. The deleteCharAt() method is used to delete single character at specific index.

Note: Here the end index is one less than what we have specified in the argument i.e. endIndex – 1.

Syntax

1

2

StringBuffer delete(intstartIndex,intendIndex)

StringBuffer deleteCharAt(intwhere)

Example

1

2

3

4

5

6

7

8

9

10

11

12

classStringBufferExample

{

publicstaticvoidmain(String...s)

{

StringBuffer str=newStringBuffer("HelloWorld");

System.out.println("before = "+str);

str.delete(0,2);

System.out.println("delete(0,2) = "+str);

str.deleteCharAt(1);

System.out.println("deleteCharAt(1) = "+str);

}

}

Output

There are so many other methods of Java StringBuffer class. If you found anything incorrect or have any doubts regarding above tutorial then please comment below.