Advertisement
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. |
//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)));;
}
}
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
Advertisement
//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)));
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement