.NET Hawa

Mutable and Immutable objects.

on December 13, 2011

While Learning a concept, my strong belif is bottom-up approch is better than top-down approch which gives clear idea. First I will start with Immutable type objects. An Immutable type object means the object which cannot change it’s state once it is created. In some cases, an object is considered immutable even if some internally used attributes change but the object’s state appears to be unchanging from an external point of view. Immutable objects are often useful because they are inherently thread safe.

Immutable objects are sole property of OOP Concepts.Before the advent of OOP, values held in program variables whose content never changed were known as ‘constants’ to differentiate them from variables that could be altered during execution. Examples might include conversion factors from kilogram weights to pounds or the value of Pi to several decimal places. In most object-oriented languages, objects can be referred to using references. Some examples of such languages are Java, C++, C#, VB.NET, and many scripting languages, such as Python and Ruby. In this case, it matters whether the state of an object can vary when objects are shared via references. If an object is known to be immutable, it can be copied simply by making a copy of a reference to it instead of copying the entire object. Because a reference (typically only the size of a pointer) is usually much smaller than the object itself, this results in memory savings and a boost in execution speed.

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered to be more thread-safe than mutable objects.

Some of the Examples in different langaugaes:

Java

A classic example of an immutable object is an instance of the Java String class.
String s = “ABC”;
s.toLowerCase();

The method toLowerCase() will not change the data “ABC” that s contains. Instead, a new String object is instantiated and given the data “abc” during its construction. A reference to this String object is returned by the toLowerCase() method. To make the String s contain the data “abc”, a different approach is needed.

s = s.toLowerCase();

Now the String s references a new String object that contains “abc”. There is nothing in the syntax of the declaration of the class String that enforces it as immutable; rather, none of the String class’s methods ever affect the data that a String object contains, thus making it immutable.
By default, fields and local variables are mutable. They can be made immutable using the final keyword.

final int j = 42;
j = 43; // does not compile

C#:

Best example for Immutable in C# is String Class.

C# Supports Immutability with two key words const and readonly. They are used by the C# compiler to ensure that the state of a field won’t be changed once an object is created.

Mutable Objects:

Mutable Objects are complety opposite to immutable objects where we can modified any where.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: