All About Class 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 sub String in middle of a String or append another String to the end.A StringBuffer class implements a mutable sequence of characters. StringBuffer are 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{
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 StringBuffer append(char c);
public StringBuffer append(char str[]);
public 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 StringBuffer append(Object obj);
public StringBuffer append(String str);
public int capacity();
public char charAt(int index);
public void ensureCapacity(int minimumCapacity);
public void getChars(int srcBegin,int srcEnd,Char dest[],int dstBegin);
public StringBuffer insert(int offSet,boolean b);
public StringBuffer insert(int offSet,char c);
public 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 StringBuffer insert(int offSet,Object obj);
public StringBuffer insert(int offSet,String str);
public int length()
public StringBuffer length();
public void setCharAt(int index,char ch);
public String toString();

All About Class StringBuffer in Java All About Class StringBuffer in Java Reviewed by Animesh Chatterjee on October 08, 2018 Rating: 5

No comments:

Powered by Blogger.