difference between String and StringBuilder

String are Immutable (Not Modifiable). If you try to modify 
the string it actually creates a new string and the old 
string will be then ready for garbage collection.

StringBuilder when instantiated, creates a new string with 
predefined capacity and upto that capacity it can 
accodomate string without needing to create a new memory 
location for the string....i mean it is modifiable and can 
also grow as and when needed. 

When the string needs to be modified frequently, preferably 
use StringBuilder as its optimized for such situations.
 
 
.........................................................
 
Both String and StringBuilder are classes used to
handle the strings.

     The most common operation with a string is
concatenation. This activity has to be performed very
efficiently. When we use the "String" object to concatenate
two strings, the first string is combined to the other
string by creating a new copy in the memory as a string
object, and then the old string is deleted. This process is
a little long. Hence we say "Strings are immutable".

     When we make use of the "StringBuilder" object, the
Append method is used. This means, an insertion is done on
the existing string. Operation on StringBuilder object is
faster than String operations, as the copy is done to the
same location. Usage of StringBuilder is more efficient in
case large amounts of string manipulations have to be performed.
 
 .................................................................
Difference..
String..
1.Its a class used to handle strings.
2.Here concatenation is used to combine two strings.
3.String object is used to concatenate two strings.
4.The first string is combined to the other string by
creating a new copy in the memory as a string object, and
then the old
string is deleted
5.we say "Strings are immutable".

String Builder..
1.This is also the class used to handle strings.
2.Here Append method is used.
3.Here, Stringbuilder object is used.
4.Insertion is done on the existing string.
5.Usage of StringBuilder is more efficient in case large
amounts of string manipulations have to be performed
 
 ............................................................



0 comments:

Post a Comment