freetimestudys@gmail.com          +91-7097633380
  Login Here                        

StringBuffer

1) String Buffer is a class present in the java.lang package.
2) StringBuffer is a final class so it can’t be inharited.
3) StringBuffer is a mutable class so it is possible to change the content in the same location.
4) StringBuffer .equals() method is used for reference comparision.
Constructors:-
   1) StringBuffer sb=new StringBuffer();
   2) StringBuffer sb1=new StringBuffer(int capacity);
   3) StringBuffer sb2=new StringBuffer(String str);

String Buffer program

class sup
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default capacity 16
StringBuffer sb1=new StringBuffer(5);
System.out.println(sb1.capacity());//your provided capacity
StringBuffer sb2=new StringBuffer("freetimestady");
System.out.println(sb2.capacity());//initial capacity+provided string length 24
System.out.println(sb2.length()); //8
}
}

intput and output

output:16,, ,,5 ,,29 ,,13

StinrgBuffer is mutable:-

Once we are creating a StringBuffer Object it is possible to the modification on existing object is called mutability nature.

String Buffer program

class sup
{
public static void main(String[] args)
{
StringBuffer s1=new StringBuffer("freetimestudy");
s1.append("hi");//mutability
System.out.println(s1);
}
};

intput and output

output:freetimestudyhi