Syntax of Java equalsIgnoreCase() Method
str2.equalsIgnoreCase(str1);
Parameters of Java equalsIgnoreCase() Method
str1, str2 - the strings to be checked for equality.
Return Value of qualsIgnoreCase() in Java
boolean
Signature of Java qualsIgnoreCase()
public boolean equalsIgnoreCase(String str)
Where str is the string to be compared with.
Internal Implementation of the equalsIgnoreCase() Method
Java
import java.lang.*;
class Main
{
public static void main(String[] args)
{
String s = "HELLO";
String t = "hELlo";
int flag=0;
if (s.length()==t.length())
{
for(int i=0;i<s.length();i++)
{
if (s.charAt(i)!=t.charAt(i))
{
if (s.substring(i,i+1).equals(t.substring(i,i+1).toUpperCase()) || s.substring(i,i+1).equals(t.substring(i,i+1).toLowerCase()))
flag=1;
else flag=0;
}
else flag=1;
}
}
else flag=0;
if (flag==0)
System.out.println("Not Equal");
else
System.out.println("Yes Equal");
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Yes Equal
Explanation
The above code sample shows how to check if one string is equal to another after ignoring their case, without using the equalsIgnoreCase() method. First, it checks if the length of the two strings is equal. Next, it checks both the strings character by character. If the characters are the same but in a different case, it converts one of the string’s characters into another case to check the equality. If none of the above conditions is satisfied then the strings are said to be unequal even after ignoring their case.
Use of Java equalsIgnoreCase()
The entire code shown above can be replaced with the equalsIgnoreCase() method.
Java
class Main
{
public static void main(String[] args)
{
String str1 = "HellO WORld";
String str2 = "hello world";
String str3 = "HELLO WORLD";
String str4 = "hello";
Boolean result;
// comparing str1 with str2
result = str1.equalsIgnoreCase(str2);
System.out.println(result);
// comparing str1 with str3
result = str1.equalsIgnoreCase(str3);
System.out.println(result);
// comparing str2 with str4
result = str2.equalsIgnoreCase(str4);
System.out.println(result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
true
true
false
Explanation
The code shown above defines four different strings - str1, str2, str3 and str4. Comparing str1, str2 and str3 with the equaleIgnoreCase() method returns true as they read the same word after ignoring the case of the individual letters. The fourth string is entirely different from the other three strings as it lacks a few characters, hence the use of equalsIgnoreCase on str4 and any of the three other strings results in false.
Example of Java String equalsIgnoreCase() Method
Example 1: Simple Case-Insensitive Comparison
Java
public class EqualsIgnoreCaseExample1 {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";
boolean result = str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Are the strings equal? true
Explanation: Although the casing is different, equalsIgnoreCase() ignores this and returns true because the contents are otherwise identical.
Example 2: Comparison with Different Words
Java
public class EqualsIgnoreCaseExample2 {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "java programming";
boolean result = str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Are the strings equal? false
Explanation: Here, the strings differ not just in case but also in content, so equalsIgnoreCase() returns false.
Example 3: Comparing Null Strings (with Safe Check)
Java
public class EqualsIgnoreCaseExample3 {
public static void main(String[] args) {
String str1 = "Rahul";
String str2 = null;
// Safe check for null before comparing
boolean result = str1 != null && str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Are the strings equal? false
Explanation: Since str2 is null, the expression str1.equalsIgnoreCase(str2) would throw a NullPointerException. By using a null check (str1 != null), we prevent this error and output false.
Frequently Asked Questions
Is Java equalsIgnoreCase null safe?
No, Java's equalsIgnoreCase() is not null-safe. If the string being compared is null, it will throw a NullPointerException. You should add null checks before using it.
What is the difference between string equals and string equalsIgnoreCase?
equals() is case-sensitive while equalsIgnoreCase() ignores case when comparing strings. For example, "Hello" equals "hello" returns false, but "Hello" equalsIgnoreCase "hello" returns true.
What does equalsIgnoreCase do in Java?
equalsIgnoreCase() compares two strings for content equality regardless of letter case. It converts both strings to the same case internally before comparing them character by character.
What is the difference between str1.equalsIgnoreCase("abc") and "abc".equalsIgnoreCase(str1), where str1 is a String?
Both the statements will return true if the value of str1 is “abc”. However, if the value of str1 is null, str1.equalsIgnoreCase("abc") will throw a NullPointerException while “abc”.equalsIgnoreCase(str1) will return false.
Conclusion
In this article, we have discussed the Java String equalsIgnoreCase() Method. The equalsIgnoreCase() method is a powerful tool in Java String handling that simplifies case-insensitive string comparisons. While it offers convenience for comparing text regardless of case, developers should remember to always implement null checks to avoid NullPointerException and consider performance implications when comparing large strings. Use equals() instead when case-sensitivity matters, and be cautious when comparing internationalized strings.
Check out our articles on: