Welcome to Sudeep Pandey

Just for sharing ideas and knowledge— My Blog

Archive for the ‘Strings’ Category

Strings are immutable

Posted by Sudeep Pandey on September 22, 2008

Strings are immutable. Immutable means that—once created—string values cannot be changed. That means, string is reference data type, so each time when we change the value of string, it builds a new object and so the previous object doesn’t point to any memory location and thus need to perform garbage collection.

To remove this problem, we have StringBuilder Class which is not immutable. So using this class, we can easily change the value on the same instance. That means it change the value on the same object.

Eg:

string str=”Hello “;
str+=”World”;
// The above example create two instance  –immutable
StringBuilder str=new StringBuilder(“Hello “);
str.Append(“World”);
//The above example create a single instance–not immutable

Posted in Strings | Leave a Comment »