Friday, May 20, 2016

Differences between String and StringBuffer and StringBuilder in Java

String :

Consider the bellow program first,

                                                 String s = new String(" Manoj");
                                                 s.concat("Patra");
                                                 System.out.println(s);  //Manoj
  1. String objects are immutable i.e once we created a String object we can't perform any changes in the existing object. 
  2. If we try to perform any change with those change a new object will be created.
  3. In the first line of the above program we are creating a string object 's' which contains 'Manoj'
  4. In the second line we are trying to do some change so, another new object will be created with content 'ManojPatra'
  5. In 3rd line we are printing 's'. Since 's' is still pointing to old object only. It will print 'Manoj'. 
Summery : Once we create a string object we can't perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This non changeable nature is nothing but immutability of string objects.

StringBuffer :


Consider the bellow program first,

                                               StringBuffer sb = new StringBuffer(" Manoj");
                                               sb.append("Patra");
                                               System.out.println(s);  //ManojPatra
  1. StringBuffer objects are mutable i.e after creating a StringBuffer object we can perform any changes in the existing object. 
  2. If we perform any changes , all those changes will be reflected in the same object.
  3. In the first line of the above program we are creating a StringBuffer object 'sb' which contains 'Manoj'
  4. In the second line we are trying to do some change so, all changes will be performed on same object.
  5. In 3rd line we are printing 'sb'. It will print 'ManojPatra'. 
Summery : Once we create a string object we can perform any type of changes in the existing object. This changeable nature is nothing but mutability of StringBuffer object.

Difference Between StringBuffer and StringBuilder?

StringBuffer is exactly same as StringBuilder ( including Methods and Construvtor) except the following difference.

StringBuffer :

  1. Every method present in StringBuffer is synchronized.
  2. At a time only one thread is allowed to operate on StringBuffer object. Hence StringBuffer object is thread safe.
  3. It increases waiting time of threads and hence relatively performance is low.
  4. Introduced in 1.0 version.

StringBuilder :

  1.  No method present in StringBuilder is synchronized.
  2. At a time multiple threads are allowed to operate on StringBuilder object. Hence StringBuilder object is not thread safe.
  3. Threads are not required to wait to operate on StringBuilder object and hence performance is relatively high.
  4. Introduced in 1.5 version.

When to use String, StringBuffer and StringBuilder?

  1. If the content is fixed and won't change frequently then we should go for String.
  2. If the content is not fixed and keep on changing but Thread safety is required then we should go for StringBuffer.
  3. If the content is not fixed and keep on changing but Thread safety is NOT required then we should go for StringBuffer.

Difference between final, finally, finalize() in Java ?

final :

  1. final is a modifier applicable for classes, methods and variables. If a class declared as final then we cant extend that class. i.e we cant create child class for that class.
  2. If a method declared as final then we can't override that method in the child class.
  3. If a variable declared as final then it will become constant and we can't perform re-assignment for that variables.

finally :

  1. finally is a block always associated with try-catch to maintain cleanup code.
                                                                   try
                                                                   {
                                                                            //risky code
                                                                   }
                                                                   catch( Exception e)
                                                                   {
                                                                            //handling code
                                                                   }
                                                                   finally
                                                                   {
                                                                              //cleanup code
                                                                   }

finalize() :

  1. finalize() is a method which is always invoked by garbage collector just before destroying an object to perform cleanup activities.
Note : Both finally and finalize() are meant for cleanup activity only but, 
  1. the finally block is responsible for cleanup activity related to try block i.e whatever resources are opened in try block will be closed by finally block.
  2. The finalize() method is associated with the object i.e all the resources related to object will be closed by finalize() method. The garbage collector is responsible for calling finalize() method just before destroying an object.