Thursday 24 July 2014

Difference between String and String Builder?

Both String and String Builder are classes used to handle 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 "String Builder" object, the Append method is used. This means, an insertion is done on the existing string. Operation on String Builder object is faster than String operations, as the copy is done to the same location. Usage of String Builder is more efficient in case large amounts of string manipulations have to be performed. Hence we say " String Builder are mutable".