GeekInterview.com
Series: Subject: Topic:
Question: 25 of 95

What is meant by Mutable and Immutable classes?

Asked by: Interview Candidate | Asked on: Mar 20th, 2007
Showing Answers 1 - 8 of 8 Answers
kvmahesh_ukt

Answered On : Mar 26th, 2007

 Immutable type are types whose instance data, fields and properties, does not change after the instance is created. Most value types are immutable, but the mutable type are A type whose instance data, fields and properties, can be changed after the instance is created. Most Reference Types are mutable. Shortly we can say as:  When you have a reference to an instance of an object, the contents of that instance can be altered  for Mutable objects and When you have a reference to an instance of an object, the contents of that instance cannot be altered  for Immutable objects.

  
Login to rate this answer.
Sanjiv Sharma

Answered On : Apr 4th, 2007

To put light on this:- When u change the value of Mutable objects the new memory is created and new value is stored in new memory.But in immutable the value is changed in same memory, this makes immutable objects to increase the performance.so in case if the string is frequently changed in the code at run time, it is advisable to use immutable objectes.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Tejas

Answered On : Aug 3rd, 2007

Hi Sanjiv, I am a bit confused here. String is a immutable class. It is advised if the value of the stringvaraible is going to change from time to time then we use a StringBuilder which is a mutable class. Hence when u change the value of as instance of string object (instance of immutable class) then new memory is created and value is stored in the new memory, where as if you assign a new value to an instance of StringBuilder class then value is changed in the same memory and this gives a performance improvement. So in case if the string is frequently changed in the code at run time, it is advisable to use mutable objectes.

  
Login to rate this answer.
amitkumar316

Answered On : Feb 18th, 2008

View all answers by amitkumar316

Immutable classes are read-only classes. once declared their value cannot be changed & its vice-versa are mutable classes.

  
Login to rate this answer.
bobby420

Answered On : Jul 30th, 2008

View all answers by bobby420

Best Example to understand Mutable and Immutable is string.string class is immutable.so if string x = "123";if you do x = x + "abc" what it does is it assigns new memory location for 123 and abc.Then adds the two strings and places the computed results in new memory location and points x to it.if you use System.Text.StringBuilder sb = new System.Text.StringBuilder("123");sb.Append("abc");x=sb.ToString();stringbuilder is mutable class. It just adds the string to same memory location.This way string manipulation is faster.I hope this helps.

Yes  1 User has rated as useful.
  
Login to rate this answer.
rajkra

Answered On : Feb 23rd, 2009

View all answers by rajkra

Instances cannot be changed but it can be accessed are called Immutable.Instances can be changed are called mutable.

  
Login to rate this answer.

String is Immutable and StringBuilder is mutable.Lets see what does that mean:String is Immutable Immutable means that cannot be overwritten.For ex: let String s="Psingla";In this case a memory (say m1)with the size of "Psingla" will allocated to String "Psingla" and reference to m1 will be stored in "s".Now if I try to puts="PsinglaTryingToChangeImmutableString";Now new memory (say m2)with the size of "PsinglaTryingToChangeImmutableString" will be allocated and the reference to m2 will be stored in "s".The m1 will be recoved by GC.Sting is mutablemutable means it can be overwrittenlet StringBuilder sb="Psingla";In this case a memory (say m1) with default size will allocated to String "Psingla" and reference to m1 will be stored in "sb".Now if I try to putsb="PsinglaTryingToChangeImmutableString";Now "PsinglaTryingToChangeImmutableString" will replace "Psingla" on same m1 memory.The m1 will be recoved by GC.

  
Login to rate this answer.
gaurav

Answered On : Oct 13th, 2012

Mutable is liable to change
Immutable is not liable to change...

String is immutable
i.e. Strings cannot be altered. When you alter a string (by adding to it for example), you are actually creating a new string.
But StringBuilder is mutable
so if u have to alter a string many times, such as mutliple concatenations then use StringBuilder.

String and StringBuilder class stores strings. But when you cannot change a String object after creating one.
eg: String name = "Prasad";
By saying you cannot change the name object means you cannot change the value in name object internally. When you change the name object value to something else, a new String object is creating in memory and assign the new value.
eg: name = "Prasad Reddy";
A new name object is creating in memory and the value "Prasad Reddy" is assinging to the newly created space.
But StringBuilder class occupies the same space even if you change the value.
If you are doing string concatenation StringBuilder class is far better in performance than String class.

  
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

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.