Java- StringBuffer and StringBuilder in Java - JavaTechWorld

Tuesday, 23 May 2017

Java- StringBuffer and StringBuilder in Java

    0

StringBuffer in Java- It is a thread safe mutable sequence of characters. A string buffer is like a string but can be modified in the same memory location.

In StringBuffer in Java class we have below 4 constructors

1.   public StringBuffer()
Creates empty StringBuffer object with default capacity 16 buffers, it means it has 16 empty location

Example:
StringBuffer sb = new StringBuffer();
System.out.println(“ sb: “ +sb);
System.out.println(“ sb capacity : “+sb.capacity()); //16

2.   public StringBuffer(int capacity)
Creates empty StringBuffer object with the given capacity

Example:
StringBuffer sb1 = new StringBuffer(3);
System.out.println(“ sb1 : “+sb1);
System.out.println(“ sb1 capacity : ”+sb1.capacity()); //3

3.   public StringBuffer(String s)
Creates StringBuffer object with given String object data. It performs string copy from String to StringBuffer object. The default capacity is [16 + s.length()]

Example:
StringBuffer sb2 = new StringBuffer(“abc”);
System.out.println(“ sb2 : “+sb2);  //abc
System.out.println(“ sb2 capacity : “+sb3.capacity());  //19

4.   public StringBuffer(CharSequence cs)
Creats new StringBuffer object with the given CS object characters, it also performs string copy from CS object to StringBuffer object. Its capacity is [16 + cs.lenght() ]

Example:
StringBuffer sb3 = new StringBuffer(“abc”);
StringBuffer sb4 = new StringBuffer(sb3);

System.out.println(“ sb3 : “+sb3); //abc
System.out.println(“ sb4 : “+sb4); //abc
System.out.println( “ sb3 capacity : “+sb3.capacity());  //19
System.out.println( “ sb4 capacity : “+sb4.capacity());  //19
System.out.println( “ sb3 == sb4);  //false
   
StringBuffer Java object creation

In the below diagram as you can see StringBuffer object is created with value "abc" and sb is a refernce variable pointing to 1010 memory location, internally StringBuffer create char[] object with size 3, once we append string buffer object with value "d" new char[] object will be created and it will copy old char[] element into new char[] with appended value "d" this process will repeaat again once we append stringbuffer object with value "e".


StringBuffer Object Diagram
StringBuilder in Java-

StringBuilder in java class is introduced in java 5 version to develop non syncronized StringBuffer object Except this there is no difference between StringBuffer and StringBuilder operation are exactly same.

Operation that we can perform on StringBuffer and StringBuilder Java classes-
  1. Append
  2. insert
  3. Delete
  4. Reverse
  5. Replacing character at given index
Java StringBuffer Example-

/** * Created by namdeva on 24/05/2017. */
public class StringBufferDemo {
    public static void main(String[] args) {
        
        //stringbuffer append        
        StringBuffer sb1 = new StringBuffer("abc");
        StringBuffer sb2 = sb1.append("d");
        System.out.print(sb1+" "+sb2);
        System.out.print(sb1==sb2);

        //stringbuffer insert        
        StringBuffer sb3 = new StringBuffer("12345");
        System.out.print(sb3); //12345
        sb3.insert(2,'/');
        System.out.print(sb3); //12/345
        
        //stringbuffer delete        
        StringBuffer sb4 = new StringBuffer("Java Tech World");
        System.out.print(sb4); // Java World

        sb4.delete(sb4.indexOf("Tech"),sb4.indexOf("Tech")+4);
        System.out.println(sb4);
        
        //stringbuffer reverse       
        StringBuffer sb5 = new StringBuffer("javatechworld");
        sb1.reverse();
        System.out.println(sb5); //dlrowhcetavaj
      //ensureCapacity
        StringBuffer sb =  new StringBuffer();
        System.out.println(sb.capacity());  //16
        sb.ensureCapacity(17);   //16*2+2=34 -> (34>17)
        System.out.println(sb); //34
    }
}

StringBuilder in Java has same StringBuffer parameter constructor as shown below-
  1. public StringBuilder()
  2. public StringBuilder(int capacity)
  3. public StringBuilder(String s)
  4. public StringBuilder(CharSequence cs)
Why StringBuIlder class is given when we have StringBuffer object ?

StringBuffer Java is syncronized objecct means mutiple threads can't access this object at a time.In single thread model application it leads performance issue because of object locking and unlocking machanism.

So StringBuilder create non-syncronized StringBuffer object to use in single thread model application with high performance.

String, StringBuffer and StringBuilder in java-

String is a immutable sequence of character object placed in " " is of type java.lang.String.
it is called as string literal.
StringBuffer in java is a thread safe mutable sequence of character object.
StringBuilder in java is a non-thread sequence of character object.

What are the difference in creating String object with literal and constructor ?
There are two difference.
  1. No. of object creation
  2. String pooling - Follow String Handling Chapter.
No. of object creation- 
String s="abc"; creates only one object.
String s1 = new String("abc"); creates two string object 
  • Argument string object if it is not yet created.
  • Current string object by copying argument string object character.
If you have any question comment below or feel free to rach out to me here

© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.