Difference Between Equals And equalsIgnoreCase In Comparing Java String Objects

Posted By : Piyush Makhija | 31-May-2018
When you use “==”, you are actually comparing the references of two object to see if they point to the same object or not. When you use “equals(…)” or "equalsIgnoreCase(...)", compares the actual string values. For example:
 
 
public class StringEquals {
 
public static void main(String[ ] args) {
 
   String s1 = "Hello";
 
   String s2 = new String(s1);
 
   String s3 = "Hello";
 
 
 
   System.out.println(s1 + " equals " + s2 + "--> " +  s1.equals(s2)); //true
 
 
 
   System.out.println(s1 + " == " + s2 + " --> " + (s1 == s2)); //false
 
   System.out.println(s1 + " == " + s3+ " --> " + (s1 == s3)); //true
 
}
 
}
 
 
 
The variable s1 refers to the String instance created by “Hello”. The object which referred to by s2 object is created with object s1 as an initializer, thus the contents of the two String objects are identical, but they are 2 different objects having 2 different references s1 and s2. This means that s1 and s2 do not refer to the same object and are, therefore, not ==, but equals( ) or equalsIgnoreCase() as they have the same value “Hello”. The s1 == s3 is true, as they both point to the same object. The references s1 and s3 are pointing to the same object in the string pool.
 
and the little difference between equals and equalsIgnoreCase is that in equals method the string needs to be exactly as we are trying to match means if a string is "Hello" so the object which we are passing in equals method that also be "Hello" but in equalsIgnoreCase the string don't need to be case sensitive.
 
public class EqualsEx{
   public static void main(String args[]){
       String string1= new String("Hello");
       String string2= new String("Hi");
       String string3= new String("Hello");
       System.out.println("string1 equals to string2:"+string1.equals(string2));
       System.out.println("string1 equals to string3:"+string1.equals(string3));
       System.out.println("string1 equals to Welcome:"+string1.equals("Welcome"));
       System.out.println("string1 equals to Hello:"+string1.equals("Hello"));
       System.out.println("string1 equals to hello:"+string1.equals("hello"));
   }
}
 
The output will be :
string1 equals to string2:false
string1 equals to string3:true
string1 equals to Welcome:false
string1 equals Hello:true
string1 equals to hello:false
 
 
where as an example for equalsIgnoreCase as below :
 
 
public class EqualsIgnoreCaseEx{
   public static void main(String args[]){
       String string1= new String("Apple");
       String string2= new String("MANGO");
       String string3= new String("APPLE");
       System.out.println("string1 equals to string2:"+string1.equalsIgnoreCase(string2));
       System.out.println("string1 equals to string3:"+string1.equalsIgnoreCase(string3));
       System.out.println("string1 equals to Welcome:"+string1.equalsIgnoreCase("Welcome"));
       System.out.println("string1 equals to Apple:"+string1.equalsIgnoreCase("Apple"));
       System.out.println("string2 equals to mango:"+string2.equalsIgnoreCase("mango"));
   }
}

 

Output will be :

string1 equals to string2:false
string1 equals to string3:true
string1 equals to Welcome:false
string1 equals to Apple:true
string2 equals to mango:true

About Author

Author Image
Piyush Makhija

'Piyush Makhija' is really good person who always accept the work. For him the work is not just like a duty but like a goal he want to achieve. His work style is different from others as because he understand the concept deeply and sometime out of box.

Request for Proposal

Name is required

Comment is required

Sending message..