Advertisement



< Prev
Next >




String Comparison with == Operator




The == operator checks for the equality of references of String objects, where as the equals() method of String class checks for the equality of values of String objects.




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


String objects created using the new keyword are stored in the normal Heap memory, where multiple String objects having the same value are allowed and each such object is different. Let us show you this with an example.

// Java - Checking equality of references of String objects creating with new keyword.


class StringEqualsOrNot
{
public static void main(String[] ar)
{
	String str1 = new String("Hello");
	String str2 = new String("Hello");


	System.out.println("First  String is : " + str1);
	System.out.println("Second String is : " + str2);


	//Comparing first String with itself
	boolean b=(str1==str1);
	System.out.println("First  String  == First  String is " + b);


	//Comparing second String with itself
	b=(str2==str2);
	System.out.println("Second String  == Second String is " + b);


	//Comparing first String with second String				
	b=(str1==str2);
	System.out.println("First  String  == Second String is " + b);

}
}


Output is :


First  String is : Hello
Second String is : Hello
First  String  == First  String is true
Second String  == Second String is true
First  String  == Second String is false


Program Analysis







Checking the equality of references of String objects in String Constant Pool.


String objects created without using the new keyword are stored in the String Constant Pool part of Heap memory, To save the space in string constant pool part of memory, there is only copy of multiple String objects having the same value is stored. Let us show you this with an example.

// Java - Checking equality of references of String objects creating without new keyword.


class StringEqualsOrNot
{
public static void main(String[] ar)
{
	String str1 = "Bonjour";
	String str2 = "Bonjour";


	System.out.println("First  String is : " + str1);
	System.out.println("Second String is : " + str2);


	//Comparing first String with itself
	boolean b=(str1==str1);
	System.out.println("First  String  == First  String is " + b);


	//Comparing second String with itself
	b=(str2==str2);
	System.out.println("Second String  == Second String is " + b);


	//Comparing first String with second String
	b=(str1==str2);
	System.out.println("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





Advertisement




Difference between == operator and equals() method for String objects.


The == operator checks for the equality of references of String objects, whereas the equals() method of String class checks for the equality of values of the String objects. Let us show you this with an example.

// Java - Difference between equals() method and == operator for String objects.


class StringEqualsOrNot
{
public static void main(String[] ar)
{
	String str1= new String("Hello");
	String str2= new String("Hello");

	System.out.println("First String is "  + str1);
	System.out.println("Second String is " + str2);


	//Comparing reference of first String with second String
	boolean b=(str1==str2);
	System.out.println("First String == Second String is " + b);


	//Comparing value of first String with second String
	System.out.println("Are values in two String object equal ? " + str1.equals(str2));
}
}


Output is :


First String is Hello
Second String is Hello
First String == Second String is false
Are values in two String object equal ? true


Program Analysis






Please share this article -




< Prev
Next >
< equals() method
compareTo() 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