Advertisement



< Prev
Next >




String Comparison with == Operator




Operator == checks for the equality of references of String objects, where as equals() method of String class checks for the equality of values in the String object.




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


String objects created using new keyword are stored in a normal Heap memory, where multiple String objects having the same value are allowed and each such object is different.
// 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 new keyword are stored in the String Constant Pool part of Heap, where, to save the memory, there is only copy of String objects having the same value.
// 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 in String.


Operator == checks for the equality of references of String objects, where as equals() method of String class checks for the equality of values in the String objects.
// Difference between equals method and == operator, performing on String.


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(str1));

}
}


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

PYTHON


Advertisement