Compare String in Java using ==, equals() and equalIgnoreCase()
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Reading time: 20 minutes | Coding time: 5 minutes
In this article, we are going to discuss about different way of comparing the Strings in java. As we know String is sequence of character and It is one the most important datatype.
Java offers different operator/ methods for comparing strings and few of them are as follows
- String comparision using == operator.
- String comparision using equals() method.
- String comparision using equalIgnoreCase() method
Explanation of above methods and operator for comparing the string in java are as follows:
Comparision Using '==' operator
- Comparision Using '==' operator:- In Java,the purpose of == operator is used to check the reference but not values.In context of String,it only checks the referential equality of the two strings variable.Referential Equality means two reference variable pointing to the same object in Java heap.Generally programmers use
==
to compare string values which is wrong.For comparing two string values equals() method is used.==
operator is only used to check two string variable pointing to the same memory location.
Implementation of code using ==
operator
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
Output
true
false
Explanation of the above code
In the above code there are three String variable s1,s2 and s3 are declared.And all the Strings have the same value. During comparision of String literal reference variable s1 and s2 point to the same object in the String pool.So the result of the comparision of s1 and s2 is true.
But during comparision of String literal reference variable s1 and an String object s3,the result is false even though they contain the same value because s3 is created using new keyword and it point to different object.So s1 and s3 point to different object and they point to same memory location.That's why the result is false.
Another important note that don't use the == for value comparision because it don't compare the values instead it only indicates whether they are pointing to the same memory location or not.
Comparision using equal() method
2.Comparision using equal() method: Unlike the == operator which compare the referential equality.The equal() method compares the String on the basis of their content or data.This method compares the two String character by character and also compare whether they are of same lenght or not.This method is defined in java.lang.Object class for reference comparision but it is overriden by java.lang.String for value based comparision.When two String compared with the equal() method,it return true
when they have same value or content and return false
when they are not equal.
Implementation of code using ==
operator
String s1 = "abc";
String s2 = "abc";
String s3 = new String("ABC");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
Output
true
false
Explaination of the above code
In above code, there are three String variable s1,s2,s3 are declared in which s1 and s2 have same value while s3 have diferent value.So when s1 and s2 are compared using equal() method it return true because they have same value/content.
But when s1 and s3 are compared using equal() method then it return false because they have different value,s1 is in lowercase while s3 is in uppercase.
Comparision using equalIgnoreCase() method
- Comparision using equalIgnoreCase() method:- This method is extention of the equal() method.This method is case-insensitive.When two String is compared using equalIgnoreCase() method, it comapred the Strings on the basis the value but perform case-insensitive comparision.It returns true/false on the basis of value ignoring case. This method comes from the java.lang.String, So it can used on String literal and an object as well.
Implementation of code using ==
operator
String s1 = "abc";
String s2 = new String("ABC");
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
Output
false
true
Explanation of the above code
In above code, two String variable s1 abd s2 are declared.when s1 and s2 is compared using equals() method, It return false because s1 and s2 are not equal by value. s1 is in lowercase while s2 is in uppercase.
But when s1 and s2 are compared using equalIgnoreCase() method, It return true because It ignores the case of the s1 and s2 and compares the string on the basis of the value.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.