Sunday, February 15, 2009

StringBuffer , StringBuilder

String objects are immutable where as StringBuffer objects are mutable.
Ex: String s=”viswan”;
s.concat(“sw”); -----> Immutability

But StringBuffer s=new StringBuffer(“viswan”);
s.append(“sw”); -----> Mutability
System.out.println(s); // viswansw

String s1=new String(“java”);
String s2=new String(“java”);
s1==s2; //false
s1.equals(s2); //false

  • Because in StringBuffer class .equals() method is not overridden for content comparision. It is meant for address comparision only.
Constructors Of StringBuffer:
StringBuffer s= new StringBuffer();
---> Creates an empty StringBuffer object with default initial capacity 16.
class SBDemo
public static void main(String a[])
StringBuffer s=new StringBuffer();
---> If we add q then //34=(16+1)*2
• When ever StringBuffer reaches its max capacity a new StringBuffer object is created with the new capacity is (current capacity + 1)*2 ---> (16+1)*2=34
• First time only this can be valid. Afterwards the capacity increases with the increase of characters only.
StringBuffer sb=new StringBuffer(int initialcapacity)
• Creates an empty StringBuffer object with the specified capacity.
StringBuffer sb=new StringBuffer(String s);
• Creates an equivalent StringBuffer object for the given String and with a capacity equal to (length of String + 16).
Ex: StringBuffer s=new StringBuffer(“durga”);
System.out.println(s.capacity()); // 5+16=21
Length of the string “durga” =5

important methods of the StringBuffer :
1. public int length() ---> returns the length of the StringBuffer.
2. public int capacity() --->returns the capacity of the StringBuffer
3. public char charAt(int index)
Ex: StringBuffer s=new StringBuffer(“durga”);
char ch= s.charAt(3); //g
char ch=s.charAt(10); //invalid index
i.e., If the index is invalid we will get a RTE saying “String index of bounds Exception”.
4. public void setCharAt(int index, char ch)
Constructing String from the given StringBuffer Object:
String s= bew String(StringBuffer s)
String s= StringBuffer.toString();
5. public synchronized StringBuffer append(String s); or (int i) or (char ch) or (float f) or (double d) or (char[] ch) or (Boolean b) or (byte[] b)….
Ex: StringBuffer s= new StringBuffer(“PI”);
s.append(3.14 f);
System.out.println(s); //pi=3.14
6. public synchronized StringBuffer insert(int offset, String s) or int/char/float/Boolean etc.,
Ex: StringBuffer s= new StringBuffer(“durga”);
System.out.println(s); //du012rga
7. public synchronized StringBuffer delete(int start, int end)

* delete the substring present in start to end – 1 position.
8. public synchronized stringBuffer delete charAt(int index)

* For deleting the character located at specified index.
9. public synchronized StringBuffer reverse()
Ex: StringBuffer sb= new StringBuffer(“madam”);
System.out.println(s.reverse()); //madam
10. public void setLength(int new length)

* This operation can result in the StringBuffer with the specified length. The Extra characters will be removed.

If the StringBuffer is lessthan the specified length padded or appended with space characters to the required length.
Ex: StringBuffer s=new StringBuffer(“viswan”);
System.out.println(s); //vis
Chaining Of Methods :
• In the case of String and StringBuffer, the return type of most of the methods are String or StringBuffer only, on that return type we are allowed to call another method, as a result chaining of methods is possible, in the case of String and StringBuffer.
Ex: StringBuffer sb= new StringBuffer(“durga”);

* All the method calls will execute left to right.
System.out.println(sb); // durgasw
--> duxxxrgasw ---> duxxasw
---> erawtfosaxxud
StringBuilder :

  • It is exactly similar to StringBuffer except all the methods are nonsynchronized methods.
  • When compared with StringBuffer the fallowing are the advantages of StringBuilder.high performance and the operation are fast.
  • Data corruption is possible in the StringBuilder which is the major drawback of StringBuilder, when compared with StringBuffer.
class Sample
public static void main(String a[])
StringBuilder sb=new StringBuilder(“surya”);
//surya //suryasw
//erawtfosayrces //erafosayrces


Post a Comment