You are on page 1of 3

MUTABLE & IMMUTABLE

  the mutable types are those whose data members can be changed after the
instance is created but Immutable types are those whose data members can not be
changed after the instance is created.
 When we change the value of mutable objects, value is changed in same memory.
But in immutable type, the new memory is created and the modified value is stored in
new memory.
STRING: -

 Strings are immutable, which means we are creating new memory every time
instead of working on existing memory.
 whenever we are modifying a value of the existing string, i.e., we are creating a
new object which refers to that modified string and the old one becomes
unreferenced.
  if we are modifying the existing string continuously, then numbers of the
unreferenced object will be increased and it will wait for the garbage collector to
free the unreferenced object.
 Immutable objects are simpler to construct, test, and use. immutable objects are
always thread-safe and etc
Example: -
string str = string.Empty;

for (int i = 0; i < 1000; i++)


{
str += "Modified ";
}

String builder: -

 StringBuilder is a mutable type, that means we are using the same memory
location and keep on appending/modifying the stuff to one instance.
 It will not create any further instances hence it will not decrease the
performance of the application.
 We have many methods for modifying the StringBuilder String as below,

1. Append - Appends information to the end of the current String Builder.


2. AppendFormat - Replaces a format specifier passed in a string with
formatted text.
3. Insert - Inserts a string or object into the specified index of the
current StringBuilder.
4. Remove - Removes a specified number of characters from the
current StringBuilder.
5. Replace - Replaces a specified character at a specified index.
Example: -
class MyClass
{
private readonly string myStr;

public MyClass(string str)


{
myStr = str;
}

public string GetStr


{
get { return myStr; }
}
}
Difference between array & ArrayList

S.No Feature Array Arraylist


1. Memory This has fixed size Size can be
and can’t increase increase or
or decrease decrease
dynamically. dynamically.
2. Namespace Array belongs to Arraylist belongs to
system.Array system.collection
namespace namespace
3. Data type In Arrays, we can In ArrayList we can
store only one store different
datatype either int, datatype variables.
string, char etc.
4. Typed rraylist are not
Arrays are strongly strongly typed.
typed which means
it can store only
specific type of
items or elements.

5. Operation speed Insertion and Insertion and


deletion operation is deletion operation in
fast arraylist is lower
than an array
6. Null Array can’t accept Arraylist can’t
null accept null
Different Between list & ArrayList

s.no Base of comparison List Arraylist

1. General It is an interface. It is class.

2. Work It creates a list of It creates a dynamic


objects that can be array that can be
accessed by the expanded when
individual index needed
number
3. Implementation List <data-type> ArrayList myList =
list1= new new ArrayList();
ArrayList();
4. Extend It extend the It extends
collection framework abstraction class
and implements the
list interface
5. Base Package java.util Java.util

6. Performance It provides faster It provides slow


manipulation of manipulation on
objects. objects compared to
List.

Different Between LinkedList & ArrayList

s.no Arraylist Linkedlist


1. 1) ArrayList internally uses LinkedList internally uses
a dynamic array to store the a doubly linked list to store
elements. the elements.
2. An ArrayList class can act LinkedList class can act as
as a list only because it a list and queue both
implements List only. because it implements List
and Deque interfaces
3. The memory location for the The location for the
elements of an ArrayList is elements of a linked list is
contiguous. not contagious.
4. To be precise, an ArrayList LinkedList implements the
is a resizable array. doubly linked list of the list
interface.
5. This class implements This class implements both
a List interface. Therefore, the List interface and
this acts as a list. the Deque interface.
Therefore, it can act as a
list and a deque.

You might also like