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"));
}
}
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
string1 equals to string3
string1 equals to Welcome
string1 equals Hello
string1 equals to hello
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"));
}
}
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"));
}
}
string1 equals to string2: false
string1 equals to string3: true
string1 equals to Welcome: false
string1 equalsto Apple: true
string2 equals to mango: true
string1 equals to string3
string1 equals to Welcome
string1 equals
string2 equals to mango
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.