BIG
DATA

JAVA

Java StringBuffer Class

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

Java StringBuffer class is used to create mutable (modifiable) string object. A string buffer is like a String, but can be modified.

As we know that String objects are immutable, so if we do a lot of modifications to String objects, we may end up with a memory leak. To overcome this we use StringBuffer class.

StringBuffer class represents growable and writable character sequence. It is also thread-safe i.e. multiple threads cannot access it simultaneously.

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

Constructors of StringBuffer class

  • StringBuffer ( ) : Creates an empty string buffer with the initial capacity of 16.
  • StringBuffer ( int capacity ) : Creates an empty string buffer with the specified capacity as length.
  • StringBuffer ( String str ) : Creates a string buffer initialized to the contents of the specified string.
  • StringBuffer ( charSequence[] ch ) : Creates a string buffer that contains the same characters as the specified CharSequence.

Important methods of StringBuffer class

append()

The append() method concatenates the given argument(string representation) to the end of the invoking StringBuffer object. StringBuffer class has several overloaded append() method.

  • StringBuffer append(String str)
  • StringBuffer append(int n)
  • StringBuffer append(Object obj)
StringBuffer strBuffer = new StringBuffer("Core");
strBuffer.append("JavaGuru");
System.out.println(strBuffer);
strBuffer.append(101);
System.out.println(strBuffer);

Output:

CoreJavaGuru
CoreJavaGuru101

insert()

The insert() method inserts the given argument(string representation) into the invoking StringBuffer object at the given position.

StringBuffer strBuffer=new StringBuffer("Core");  
strBuffer.insert(1,"Java");
System.out.println(strBuffer);

Output:

CJavaore

replace()

The replace() method replaces the string from specified start index to the end index.

StringBuffer strBuffer=new StringBuffer("Core");  
strBuffer.replace( 2, 4, "Java");
System.out.println(strBuffer);

Output:

CoJava

reverse()

This method reverses the characters within a StringBuffer object.

StringBuffer strBuffer=new StringBuffer("Core");  
strBuffer.reverse();
System.out.println(strBuffer);

Output:

eroC

delete()

The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.

StringBuffer strBuffer=new StringBuffer("Core");  
strBuffer.delete( 2, 4);
System.out.println(strBuffer);

Output:

Co

capacity()

The capacity() method returns the current capacity of StringBuffer object. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

StringBuffer strBuffer=new StringBuffer();  
System.out.println(strBuffer.capacity());
strBuffer.append("1234");
System.out.println(strBuffer.capacity());
strBuffer.append("123456789112");
System.out.println(strBuffer.capacity());
strBuffer.append("1");
System.out.println(strBuffer.capacity()); //(oldcapacity*2)+2  

StringBuffer strBuffer2=new StringBuffer("1234");  
System.out.println(strBuffer2.capacity());

Output:

16
16
16
34
20

Observe how the capacity changes.

ensureCapacity()

The ensureCapacity() method of StringBuffer class ensures that the given capacity is the minimum to the current capacity. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:

  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.

If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

StringBuffer strBuffer=new StringBuffer("Core");  
strBuffer.ensureCapacity(10);
System.out.println(strBuffer.capacity());

StringBuffer strBuffer2=new StringBuffer("Core");  
strBuffer2.ensureCapacity(30);
System.out.println(strBuffer2.capacity());

Output:

20
42

Difference between String and StringBuffer

Some of differences between String and StringBuffer are given below:

String StringBuffer
String class is immutable. StringBuffer class is mutable.
String are stored in Sting pool. StringBuffers are stored in heap.
While performing concatenations, String is slow because you are actually creating new object(internally) every time since String is immutable. StringBuffer is fast and consumes less memory when you cancat strings.
You can compare the contents of two strings by equals() method as String class overrides the equals() method of Object class. StringBuffer class doesn't override the equals() method.