GeekInterview.com
Series: Subject: Topic:
Question: 125 of 460

Why strings are immutable?

Asked by: Rohit Sharma | Member Since Apr-2006 | Asked on: Apr 21st, 2006

View all questions by Rohit Sharma   View all answers by Rohit Sharma

Showing Answers 1 - 9 of 9 Answers
veeresh

Answered On : May 6th, 2006

View all answers by veeresh

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.

  
Login to rate this answer.
Ramon Casha

Answered On : May 26th, 2006

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

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Ashishtandon.e

Answered On : May 29th, 2006

View all answers by Ashishtandon.e

In .NET, strings are immutable. This means that, once a value is assigned to a String object, it can never be changed. Thats right you cant change a Strings 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 didnt! Lets 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 youre 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 youd change the content of *ALL* other strings pointing to that location! This is the reason for strings immutability.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Prashant

Answered On : 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

  
Login to rate this answer.
prudhviram

Answered On : Sep 14th, 2006

View all answers by prudhviram

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.

  
Login to rate this answer.
KMD

Answered On : 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.

  
Login to rate this answer.
nikhilrawat23

Answered On : May 28th, 2008

View all answers by nikhilrawat23

In .netCLR maintain string pool for handling string operation.CLR not make any new string object bt it uses string object referencefrom 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...........

  
Login to rate this answer.
LordAlex

Answered On : Nov 6th, 2011

View all answers by LordAlex

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.

  
Login to rate this answer.
Jeevan

Answered On : Apr 20th, 2012

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.