Advertisement



< Prev
Next >



C# Intern() method




To minimize the redundancy and memory waste/overuse caused by storing String literals with duplicate values on the heap, the common language runtime(CLR) reserves a special table on the Heap memory called intern pool, which stores only a single reference to each unique String literal declared in a program.

The Intern() method of String class searches for a String in the intern pool table and returns its reference, if it is found. For a detailed explanation of intern pool table, please refer to our tutorial - Intern Pool Table.


The String object created by the direct assigning of a String literal are stored in the String intern pool table, while the dynamically created String objects are stored in the normal non-intern pool heap memory.








Explaining the Intern() method


The Intern() method is a static method of String class and can be called just with the name of its class and a dot operator.

Methods Description
static Intern( String str)
This method searches for a String in the intern pool table and returns its reference (if the searched String literal value is found i.e. interned), otherwise, a reference to the searched String is added to the intern pool and that reference is returned.






Calling the Intern(String) on non-dynamically created String objects


In the upcoming code, we are going to create multiple String objects by the direct assigning of String literals, which will store their references in the intern pool table.

Next, we will call the ReferenceEquals() static method of Object class, which checks for equality of references and within the ReferenceEquals() method, we will call the Intern() method to retrieve the reference to a String object, so that we can verify if the reference to the String object is contained in the intern pool table or not.

//C# Example of the Intern() method of the String class.

using System;

class InternEx
{
public static void Main()
{
	//Creating the first String object
	String str1 = "Hello world!";

	//Creating the second String object with the same value as the first String
	String str2 = "Hello world!";

	//Creating the third String object
	String str3 = "Always try to think positive in life";

	//Creating the fourth String object with the same value at the third String
	String str4 = "Always try to think positive in life";
	
	//Printing the String objects
	Console.WriteLine("str1: "  + str1);
	Console.WriteLine("str2: "  + str2);
	Console.WriteLine("str3: "  + str3);
	Console.WriteLine("str4: "  + str4);

	//Comparing the reference of str1 with str2 to see if they point of the same String object in intern pool table
	Console.WriteLine("Is str1 the same reference as str2?: " +  ReferenceEquals(String.Intern(str1), String.Intern(str2)));

	//Comparing the reference of str3 with str4 to see if they point of the same String object in intern pool table
	Console.WriteLine("Is str3 the same reference as str4?: " +  ReferenceEquals(String.Intern(str3), String.Intern(str4)));;
}
}


Output is :


str1: Hello world!
str2: Hello world!
str3: Always try to think positive in life
str4: Always try to think positive in life
Is str1 the same reference as str2?: True
Is str3 the same reference as str4?: True



Program Analysis





Advertisement




Calling the Intern() method on non-dynamic and dynamically created String objects


In the upcoming code, we are going to dynamically create multiple String objects, which will store their references in the normal heap memory, and we are going to statically create a String object by directly assigning a String literal, which will be stored in the intern pool table.

Next, we will call the ReferenceEquals() static method of Object class, which checks for equality of references between the statically and dynamically created String object and within the ReferenceEquals() method, we will call the Intern() method to retrieve the reference of each String object that we want to compare, so that we can verify if the reference to the String value is contained in the intern pool table or not.

//C# Example of the Intern() method of the String class.

using System;
using System.Text;

class InternEx
{
public static void Main()
{
	//Statically Creating the first String object by directly assigning a String literal
	String str1 = "Be Happy";

	//Creating a char array
	char[] arr = {'B', 'e', ' ', 'H', 'a', 'p', 'p', 'y'};
	
	//dynamically creating the second String object from the char[] array, 
	String str2 = new String(arr);

	//dynamically creating the third String object from StringBuilder 
	String str3 = new StringBuilder("Be Happy").ToString();
	
	//dynamically creating the fourth String object from StringBuilder 
	String str3 = new StringBuilder("Be Kind").ToString();

	
	//Printing the String objects
	Console.WriteLine("str1: "  + str1);
	Console.WriteLine("str2: "  + str2);
	Console.WriteLine("str3: "  + str3);
	Console.WriteLine("str4: "  + str4);
	

	//Comparing the reference of str1 with str2 to see if they point to an existing String literal value in intern pool table
	Console.WriteLine("Is str1 the same reference as str2?: " +  ReferenceEquals(String.Intern(str1), String.Intern(str2)));


	//Comparing the reference of str1 with str3 to see if they point to an existing String literal value in intern pool table
	Console.WriteLine("Is str1 the same reference as str3?: " +  ReferenceEquals(String.Intern(str1), String.Intern(str3)));


	//Comparing the reference of str2 with str3 to see if they point to an existing String literal value in intern pool table
	Console.WriteLine("Is str2 the same reference as str3?: " +  ReferenceEquals(String.Intern(str2), String.Intern(str3)));
	
	//Comparing the reference of str2 with str4 to see if they point to an existing String literal value in intern pool table
	Console.WriteLine("Is str2 the same reference as str4?: " +  ReferenceEquals(String.Intern(str2), String.Intern(str4)));


	//Comparing the reference of str3 with str4 to see if they point to an existing String literal value in intern pool table
	Console.WriteLine("Is str3 the same reference as str4?: " +  ReferenceEquals(String.Intern(str3), String.Intern(str4)));
}
}


Output is :


str1: Be Happy
str2: Be Happy
str3: Be Happy
str4: Be Kind
Is str1 the same reference as str2?: True
Is str1 the same reference as str3?: True
Is str2 the same reference as str3?: True
Is str3 the same reference as str4?: False



Program Analysis


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