Saturday 6 September 2014

Difference between String ,String Builder and String Buffer



                                                           


String :

 String is immutable object i.e. once created cannot be changed.
object created is stored in Constant Spring pool.
Immutable objects means they can't be modified.
They are thread safe means can be used by only single thread at a time.

e.g. String value1="test";

this object is stored in constant pool and value cannot be modified.

How to reverse a string in java

StringBuffer :


StringBuffer is mutuable i.e. values can be changed.

It is defined as

String value1= new String ("test");

These values are stored in heap and can be modified at later point  of time

i.e. value1=new String ("testing");

now value1 is referring to an object which has value "testing".

 String Buffer is also synchronized means thread-safe.

Difference between Application Server and Web server 
String Builder :

String builder is also as same as String Buffer .It also stores value in heap.But unlike StringBuffer , String Builder is non synchronized which makes it working faster.


You might like :

Custom Exception in Java -Tutorial
Life Cycle of a thread 
How to reverse a string in java

No comments:

Post a Comment