What is meant by Mutable and Immutable classes?

Showing Answers 1 - 8 of 8 Answers

kvmahesh_ukt

  • 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.

  Was this answer useful?  Yes

Sanjiv Sharma

  • 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.

Tejas

  • Aug 3rd, 2007
 

Hi Sanjiv, I am a bit confused here.

String is a immutable class. It is advised if the value of the string
varaible 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.

  Was this answer useful?  Yes

bobby420

  • Jul 30th, 2008
 

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.

rajkra

  • Feb 23rd, 2009
 

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

  Was this answer useful?  Yes

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 put
s="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 mutable
mutable means it can be overwritten
let 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 put
sb="PsinglaTryingToChangeImmutableString";
Now "PsinglaTryingToChangeImmutableString" will replace "Psingla" on same m1 memory.
The m1 will be recoved by GC.

  Was this answer useful?  Yes

gaurav

  • 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.

  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.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions