Monday 17 February 2014

java.lang.String, StringBuffer and StringBuilder

String:

String objects are immutable. This means even if you have reference to any String object, contents of that object can't be modified or altered. If you perform any operation like concat(), replace (), substring() etc on String object, a new String  object reference is returned.

For example :--

String s = "abcd"; //a new String object is created with value "abcd" on string pool in heap. See below diagram:





Now lets do concat operation string object s which we created earlier.

s =  s.concat("ef");

Here on doing contact operation, a new object reference is returned with value "abcdef "and reference is assigned to s. Now s holds a reference to a new String object in String pool whose value is "abcdef" and earlier String literal is still present in String pool.

This means String objects are immutable means when ever you do any manipulation with Strings, result of that operation will always be a new String object. 

Points to Note:-

1.  Here String Object is immutable not the reference.

2. String class is made final to avoid String class being extended to override behavior of String methods.

3. String s  = new String("abcd");

This statement will create two String object. The new operator will create a new object in non pool area on heap and  literal "abcd"  will also be placed on String pool.
 

StringBuffer:

why StringBuffer class when Java  already have String class ?

As mentioned above, String class is immutable and if you need to do lots of manipulations with String objects, then lots of unused abandoned objects will be created in String pool. Hence StringBuffer is added. 

 StringBuffer objects are mutable, so they can be modified. StringBuffer class provides methods which can directly manipulate underlying string object. Hence, any manipulation of underlying string doesn't result in to creation of new String Object.

Points related to StringBuffer size :--

1. In general a StringBuffer object will be created with a default capacity of 16 characters.

StringBuffer sb =  new StringBuffer(); // 16 characters

2. We can set the size as :--

StringBuffer sb = new StringBuffer (30); //30 characters
In this case, StringBuffer object is created as an empty object with a capacity for storing 30 characters

Even if we declare the capacity as 30, it is possible to store more than 30 characters into StringBuffer. To store characters, we can use append () method as:
sb.append (“XYZSSS”);

StringBuffer is thread safe means, all the methods of StringBuffer are synchronized hence only one thread at a time can access the StringBuffer instance for manipulations.
  
StringBuilder:

StringBuilder was added in Java 1.5 to provide String manipulations as compared to StringBuffer.

StringBuilder has exactly same methods as StringBuffer class but these methods are not synchronized hence StringBuilder provides faster String manipulations.    

  



  


No comments:

Post a Comment