Java StringBuffer Class

In this tutorial you will learn about Java StringBuffer class and its important methods with examples.

StringBuffer in Java is used to create mutable strings. Here mutable means, we can modify or change the strings.

StringBuffer class is thread safe which means multiple threads can’t access the same object simultaneously.

Apart from StringBuffer, we can also make mutable strings using StringBuilder class. We will discuss about it in upcoming tutorials.

 

Java StringBuffer Constructors

StringBuffer class in Java provides following four constructors.

StringBuffer() – Creates an empty string with the default capacity of 16 characters.

StringBuffer(int size) – Creates an empty string with the specified capacity.

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

int length()
int capacity()

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("Hello");
		System.out.println("length = "+str.length());
		System.out.println("capacity = "+str.capacity());
	}
}

 

Output

StringBuffer length() and capacity() Methods

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

void ensureCapacity(int minCapacity)

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("Hello");
		System.out.println("capacity = "+str.capacity());
		str.ensureCapacity(30);
		System.out.println("capacity = "+str.capacity());
	}
}

 

Output

StringBuffer ensureCapacity() Method

 

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

void setLength(int len)

 

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

char charAt(int where)
void setCharAt(int where, char ch)

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("Hello");
		System.out.println("before = "+str);
		str.setCharAt(1,'a');
		System.out.println("after = "+str);
	}
}

 

Output

StringBuffer charAt() and setCharAt() Methods

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

StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("Hello");
		System.out.println("before = "+str);
		str.append("World");
		System.out.println("after = "+str);
	}
}

 

Output

StringBuffer append() Method

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

StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("TheProgrammer");
		System.out.println("before = "+str);
		str.insert(3,"Crazy");
		System.out.println("after = "+str);
	}
}

 

Output

StringBuffer insert() Method

StringBuffer reverse() Method

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

 

Syntax

StringBuffer reverse()

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("TheCrazyProgrammer");
		System.out.println("before = "+str);
		str.reverse();
		System.out.println("after = "+str);
	}
}

 

Output

StringBuffer reverse() Method

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

StringBuffer replace(int startIndex, int endIndex, String str)

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("TheCrazyProgrammer");
		System.out.println("before = "+str);
		str.replace(8,18,"Coder");
		System.out.println("after = "+str);
	}
}

 

Output

StringBuffer replace() Method

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

StringBuffer delete(int startIndex, int endIndex)
StringBuffer deleteCharAt(int where)

 

Example

class StringBufferExample
{
	public static void main(String...s)
	{
		StringBuffer str=new StringBuffer("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

StringBuffer delete() and deleteCharAt() Method

 

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.

Leave a Comment

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