Saturday, August 21, 2010

Difference in String and StringBuilder

String objects are immutable, which means that once they are created they cannot be changed. When we use one of the methods of the String class, we create a new string object.

For example: (String strSen = “hello”; //Creates a new object).

When we concatenate any other words along with strSen variable it does not actually modify the strSen variable, instead it creates a whole new string.

For example: strSen = strSen + “Hello”, create a new string.


StringBuilder are mutable class, which means when concatenate any text to the variable it allows us to modify the original value without creating a new object.

You can use StringBuilder's Append () method to use concatenation.