Why strings are immutable?

Questions by Rohit Sharma   answers by Rohit Sharma

Showing Answers 1 - 7 of 7 Answers

veeresh

  • May 6th, 2006
 

1) In VB6.0 and c++ strings are simple array of bytes, but in .Net it is class System.String and is immutable.

2) A string is a sequential collection of Unicode characters, which usually is used to represent text. A String, on the other hand, is a .NET Framework class (System.String) that represents a Unicode string with a sequential collection of System.Char structure.

Ramon Casha

  • May 26th, 2006
 

Because they are immutable in Java, and .NET/C# are copied from Java

In .NET, strings are immutable. This means that, once a value is assigned to a String object, it can never be changed. That?s right ? you can?t change a String?s value! Take a look at this code:1 class Test2 {3 public static void Main()4 {5 string myString = "1234";6 System.Console.WriteLine(myString);7 myString += "5678";8 System.Console.WriteLine(myString);9 }10 }The output from this is:123412345678Though it seems as if we just changed the value of myString from ?1234? to ?12345678?, we really didn?t! Let?s step through the above code. In line 5, a new String object is allocated on the heap with a value of ?1234?, and myString points to its memory address. In line 7, a new string is once again allocated on the heap, with value ?12345678?, and myString now points to this new memory location. So you actually sit with two string objects on the heap, even though you?re only referencing one of them. The ?1234? string is still interned, and if unused, it will be garbage collected with the next GC cycle.If you now create any number of string objects, all with a value of ?1234?, they would all point to the one interned instance. This ensures that strings use memory very efficiently.When instantiating a string object with the value of ?1234?, your string could thus be pointing to the same location as other already existing strings are. Now, imagine the chaos you could cause by changing the content of your string ? you?d change the content of *ALL* other strings pointing to that location! This is the reason for strings? immutability.

Prashant

  • Jul 25th, 2006
 

Hi,

Kindly don't answer so vaguely. It's never logical to say that if this is like this in some thing so it is same as that.

Mind well every thing implemented has some reasons behind.

I conduct interview so many times, and frankly I get irritated by such answers.

Think of your real interview... frankly it will ruin out your whole effort to conquer interview.

Thanks,

Prashant

  Was this answer useful?  Yes

prudhviram

  • Sep 14th, 2006
 

Ramon Casha. Dont give wrong or incorrect answer and mislead people ok you better try to read answers never try to give answers. I will really appreciate it.

  Was this answer useful?  Yes

KMD

  • Sep 25th, 2006
 

String is of reference type which holds an array of char.

string s = "abc";

internally creates an array of char similar to one below

char[] s = new char[] {'a','b','c'};

Arrays are created with definite length and it cannot be dynamically increased.

This is the reason behind immutability in string.

  Was this answer useful?  Yes

In .net?CLR maintain string pool for handling string operation.CLR not make any new string object bt it uses string object reference?from string pool.when we define new string object with same array of character then CLR assign same memory reference to that instance which was previously given to previous string object for the sake of memory management.and if we make any change in first string object then it will reflect on second one also so to overcome from this problem the string? is immutable.
and if u want mutable string then u have to use string builder class...........

  Was this answer useful?  Yes

LordAlex

  • Nov 6th, 2011
 

String is an array of bytes.
When are you ave to change it, you have to create the new one; because the string is always considered as a constant.

  Was this answer useful?  Yes

Jeevan

  • Apr 20th, 2012
 

People, please read the question before answering, don't just answer "Why" is the question - not "how".

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions