Advertisement



< Prev
Next >



C# String Immutability






What is String immutability?


One of the important features of a String object is its immutability, which means String objects are immutable, in other words, once you have created a String object its value cannot be changed or modified.




How is a String object immutable?


Now let's understand this with an example -:

//Creating two String objects in the intern pool 
//by directly assigning a String literal value
String  s1 = "Sky";  // Statement1
String  s2 = "Blue"  // Statement2 

Figure 1 below shows that after executing the two statement above, we have two String objects Sky and Blue in the intern pool table, which are referenced by reference variables s1 and s2 existing on the Stack.





We also know that two String objects can be added using the + operator. So, let's try to add these two String objects in order to create a new String object, which will land this new String object in the normal non-intern pool heap memory(as it is created dynamically), as depicted below.

//Dynamically creating a String object 
//by programmatically adding two String objects
s1 = s1 + s2; 	// Statement3 




Note : Dashed-line indicates an old reference which doesn't exist anymore.

As you may see in the Figure 2, after executing the statement3, reference variable s1 points to a new String object SkyBlue, while the original String object Sky is left unmodified.

We have 3 String objects on the heap memory after executing statement3 -

This proves that String objects are immutable, because modification of a String object Sky by adding another String object Blue to it, didn't modify the original String object Sky but it rather created a new String object, SkyBlue.


Advertisement




Are reference variables immutable too?


As you may see in the Figure 1, reference variable s1 was initially assigned to a String object Sky but in the Figure 2, reference variable s1 is pointing to a different object SkyBlue. Hence, only a String object is immutable, while its reference variable can be modified.





String immutability works for all the String objects.


Here in code below, we are creating 4 String objects, two of them are going to be stored in the String intern pool table, while the other two dynamically created String objects are going to be stored in the normal heap memory, and we are going to prove that String immutability works for all String objects, whether they exist in String intern pool table or in the normal heap memory.




String Immutability Example


//C# String Immutability Example 

using System;
using System.Text;

class String1
{
public static void Main()
{

	//Creating two String objects in the intern pool 
	//by directly assigning a String literal value
	String s1= "Sky"; 
	String s2= "Blue";

	//Dynamically creating a String object 
	//by programmatically adding two String objects
	s1= s1+s2; 	  //Statement 1

	Console.WriteLine("s1 points to " + s1);
	Console.WriteLine("s2 points to " + s2);


	//Creating a char array
	char[] arr = {'N', 'e', 'w'};


	//Dynamically creating a String object from a char[] array.
	String s3 = new String(arr);


	//Dynamically creating a String object by calling converting a StringBuilder to String object.
	String s4 = new StringBuilder("York").ToString();


	//Programmatically reinitializing the value of a String object 
	//referenced by, s3
	s3= s3+s4; 	  //Statement 2

	Console.WriteLine("s3 points to " + s3);
	Console.WriteLine("s4 points to " + s4);
}
}


Output is :


s1 points to SkyBlue
s2 points to Blue
s3 points to NewYork
s4 points to York

After initially pointing to the String object New in the normal heap memory, the reference variable s3 now points to a new String object NewYork stored in the heap memory. Hence, there is no reference variable pointing to String object New and it's lost. While, the reference variable s4 still points to York on normal heap memory.



Please share this article -





< Prev
Next >
< C# String intern pool
C# String methods >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement