Advertisement



< Prev
Next >




C# String Comparison with == Operator




Normally, the == operator checks for the equality of references for all the objects except for the String objects. When comparing the String objects, the == operator checks for the equality of values of the String objects and not the equality of references of String objects.




Checking the equality of values of the String objects in Heap memory.


Dynamically created String objects are stored in a normal Heap memory, where multiple String objects having the same value are allowed and each dynamically created object has a different reference.

// Checking for the equality of values of dynamically created String objects using == operator 

using System;


class StringEqualsOrNot
{
public static void Main(String[] str)
{
	//Creating a char array
	char[] arr = {'N', 'e', 'w', ' ', 'Y', 'o', 'r', 'k'};

	//Creating the first String object from a char[] array.
	String str1 = new String(arr);

	//Creating the Second String object from the same char[] array.
	String str2 = new String(arr);
	
	//Printing the String objects
	Console.WriteLine("First  String is : " + str1);
	Console.WriteLine("Second String is : " + str2);


	//Comparing first String with itself
	bool b = (str1==str1);
	Console.WriteLine("First  String  == First  String is " + b);


	//Comparing second String with itself
	b = (str2==str2);
	Console.WriteLine("Second String  == Second String is " + b);


	//Comparing first String with second String
	b = (str1==str2);
	Console.WriteLine("First  String  == Second String is " + b);

}
}


Output is :


First  String is : New York
Second String is : New York
First  String  == First  String is True
Second String  == Second String is True
First  String  == Second String is True


Program Analysis





Advertisement




Checking the equality of values of String objects in String Intern Pool Table.


String objects which are not dynamically created are stored in the String Intern Pool Table, where, to save the memory, only the single unique copies of String objects are stored.

// Checking equality of values of non-dynamically created String objects 

using System;

class StringEqualsOrNot
{
public static void Main(String[] str)
{
	//Creating two String objects(non-dynamically) by directly assigning String literal
	String str1 = "Bonjour";
	String str2 = "Bonjour";


	//Printing two String objects
	Console.WriteLine("First  String is : " + str1);
	Console.WriteLine("Second String is : " + str2);

	
	//Comparing first String with itself
	bool b = (str1==str1);
	Console.WriteLine("First  String  == First  String is " + b);


	//Comparing second String with itself
	b = (str2==str2);
	Console.WriteLine("Second String  == Second String is " + b);


	//Comparing first String with second String
	b = (str1==str2);
	Console.WriteLine("First  String  == Second String is " + b);
}
}


Output is :


First  String is : Bonjour
Second String is : Bonjour
First  String  == First  String is True
Second String  == Second String is True
First  String  == Second String is True


Program Analysis






Please share this article -




< Prev
Next >
< C# String Methods
C# Concat() Method >



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