In Java, equalsIgnoreCase() checks whether two strings have the same letters when case is ignored. It returns true or false.
You pass the other string. It is required. Whitespace still matters. For sort order ignoring case, use compareToIgnoreCase().
Syntax
Call equalsIgnoreCase on the string. Start with this line.
java
boolean ok = a.equalsIgnoreCase(b);
Call
What it does
a.equalsIgnoreCase(b)
Returns true when a and b match ignoring letter case.
This is for equality, not sorting. Prefer compareToIgnoreCase() when you need order.
Library declarations
java
public boolean equalsIgnoreCase(String anotherString);
Parameters
Parameter
Description
anotherString
Required. The string to compare with this instance.
Return Value
Return Value
Description
true
The strings match when case is ignored.
false
The strings do not match when case is ignored.
Java Examples
Example 1 — Basic equalsIgnoreCase
Confirm two strings match when only capitalization differs.
java
public class EqualsIgnoreCaseBasic {
public static void main(String[] args) {
String a = "Java";
String b = "java";
boolean ok = a.equalsIgnoreCase(b);
System.out.println("equalsIgnoreCase = " + ok);
}
}
Output
equalsIgnoreCase = true
How It Works
Store both strings.a and b differ only in the first letter’s case.
Call equalsIgnoreCase. Java compares after treating case as equivalent.
Get true. Every position matches when case is ignored.
Print the boolean. Useful for quick checks in demos and tests.
Example 2 — equals vs equalsIgnoreCase
Side-by-side with equals() so the case rule is obvious.
java
public class EqualsVsIgnoreCase {
public static void main(String[] args) {
String left = "YES";
String right = "yes";
System.out.println("equals = " + left.equals(right));
System.out.println("equalsIgnoreCase = " + left.equalsIgnoreCase(right));
}
}
Output
equals = false
equalsIgnoreCase = true
How It Works
Pick two spellings."YES" and "yes" are the same word in different case.
Try equals. Exact code-unit match fails → false.
Try equalsIgnoreCase. Case is ignored → true.
Choose by intent. Passwords usually need equals; commands often want ignore-case.
Example 3 — Accept y / Y / yes
Put the literal on the left so a null answer cannot cause NullPointerException. Prefer this over manual toLowerCase() for simple checks.
java
public class EqualsIgnoreCaseInput {
public static boolean isYes(String answer) {
return "y".equalsIgnoreCase(answer)
|| "yes".equalsIgnoreCase(answer);
}
public static void main(String[] args) {
System.out.println(isYes("Y"));
System.out.println(isYes("Yes"));
System.out.println(isYes("no"));
System.out.println(isYes(null));
}
}
Output
true
true
false
false
How It Works
Literal on the left."y".equalsIgnoreCase(answer) is safe if answer is null.
Allow short or long yes. Match "y" or "yes" in any case.
Reject other text."no" matches neither → false.
Null returns false. No exception—handy for optional input.
Frequently Asked Questions
equalsIgnoreCase() checks whether two strings match while ignoring letter case.
equals() is case-sensitive. equalsIgnoreCase() ignores case.
anotherString is required.
Yes. Spaces and tabs still have to match.
No. Use compareToIgnoreCase() when you need order.