All About Class StringBuffer in Java

Spread the love
  • 1

StringBuffer in Java:

StringBuffer is a peer class of String. While string creates Strings of fixed length, StringBuffer creates Strings of flexible length that can be modified in terms of length and content. We can insert characters and subString in middle of a String or append another String to the end. A StringBuffer class implements a mutable sequence of characters. StringBuffer is safe for use by multiple threads. The methods are synchronized wherever necessary so that the operations on any particular instance behave as if they occur in some serial order. StringBuffer is used by the compiler to implement binary string concatenation (for operator +).

//is compiled and equivalent to
x=new StringBuffer().append(“a”).append(4).append(“c”).toString();

The principal operation on a StringBuffer are as follows:

  1. The append()-append method adds the characters at the end of the buffer
  2. The insert()- insert method adds the character at a specified point.


StringBuffer z=new StringBuffer("Love");
z.append("ly");// will give Lovely
z.insert(2,"le");//will give Loleve 

They are overloaded to accept data of any type. Each effectively converts a given data to that String to StringBuffer. Every StringBuffer has a capacity. As long as the length of the character sequence contained in the StringBuffer 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.

The structure of the class StringBuffer is given as:

public class java.lang.StringBuffer extends 
             java.lang.Object implements
public StringBuffer()// constructs a StringBuffer with 
//no character.Initial capacity is 16 characters.
public StringBuffer(int length)// constructs a StringBuffer
// with no character.The length is specified as the 
//length given in the argument.It can throw 
//NegativeArraySizeException if the lenth argument is less
// than zero.
public StringBuffer(String str)// constructs a StringBuffer
// so that it represent the same sequence of 
//characters as the String str.Initial capacity is 16 
//characters+length of the String str.
public StringBuffer append(boolean b);
public synchronized StringBuffer append(char c);
public synchronized StringBuffer append(char str[]);
public synchronized StringBuffer append(char str[],int offSet,int len);
public StringBuffer append(double d);
public StringBuffer append(float f);
public StringBuffer append(int i);
public StringBuffer append(long l);
public synchronized StringBuffer append(Object obj);
public synchronized StringBuffer append(String str);
public int capacity();
public synchronized char charAt(int index);
public synchronized void ensureCapacity(int minimumCapacity);
public synchronized void getChars(int srcBegin,int srcEnd,Char dest[],int dstBegin);
public StringBuffer insert(int offSet,boolean b);
public synchronized StringBuffer insert(int offSet,char c);
public synchronized StringBuffer insert(int offSet,char str[]);
public StringBuffer insert(int offSet,double d);
public StringBuffer insert(int offSet,float f);
public StringBuffer insert(int offSet,int i);
public StringBuffer insert(int offSet,long l);
public synchronized StringBuffer insert(int offSet,Object obj);
public synchronized StringBuffer insert(int offSet,String str);
public int length()
public StringBuffer length();
public synchronized void setCharAt(int index,char ch);
public synchronized StringBuffer reverse();
public synchronized void setLength(int newLength);
public String toString();

How to convert StringBuffer to String?

After a String is processed in a StringBuffer object, it may be efficiently converted to a String object for further use. The StringBuffer.toString() method does not really copy the internal array of characters, rather it shared that array with the new String object and makes a new copy for itself. We can then further do any modifications and operations.


Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1


Please enter your comment!
Please enter your name here