The equalsIgnoreCase() method compares two strings for the same text while ignoring letter case. It returns true or false—perfect when users might type yes, YES, or Yes.
01
Ignore Case
A = a for compare.
02
Returns boolean
true or false.
03
vs equals()
Case-sensitive peer.
04
User Input
Commands, flags.
05
Null Safe Arg
ignoreCase(null) → false.
06
Full String
Not substring search.
Fundamentals
Definition and Usage
In Java, equalsIgnoreCase(String anotherString) is defined on java.lang.String. It compares the full content of two strings character by character, treating uppercase and lowercase forms of the same letter as equal. If "Hello, World!" is compared with "HELLO, world!", the result is true.
Use this method when case differences should not affect the outcome—such as accepting configuration values, menu choices, or file extensions typed in mixed case.
💡
Beginner Tip
When case does matter (passwords, exact codes), use equals() instead. When case should not matter, use equalsIgnoreCase().
Foundation
📝 Syntax
The equalsIgnoreCase() method is declared in the String class:
java
public boolean equalsIgnoreCase(String anotherString)
Parameters
anotherString — the string to compare with. May be null; a null argument returns false.
Return Value
Returns true if both strings are equal when case is ignored; otherwise false.
Exceptions
Does not throw when the argument is null. Calling the method on a null string reference throws NullPointerException.
Cheat Sheet
⚡ Quick Reference
Expression
Result
"Java".equalsIgnoreCase("java")
true
"Java".equals("java")
false (case-sensitive)
"Hello".equalsIgnoreCase("HELLO")
true
"Hi".equalsIgnoreCase("Hello")
false (different text)
"Yes".equalsIgnoreCase(null)
false
Command
input.equalsIgnoreCase("quit")
QUIT, quit, Quit
Null-safe
"yes".equalsIgnoreCase(input)
input may be null
Strict match
str1.equals(str2)
When case matters
Ordering
s1.compareToIgnoreCase(s2)
Sort, not just equal
Hands-On
Examples Gallery
These programs run in Java 8+. They show a case-insensitive match, comparison with equals(), user command handling, null safety, and contrast with compareToIgnoreCase().
📚 Getting Started
Compare two strings that differ only in capitalization.
Example 1 — Case-Insensitive Match
"Hello, World!" and "HELLO, world!" are equal when case is ignored.
java
public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "HELLO, world!";
boolean result = str1.equalsIgnoreCase(str2);
if (result) {
System.out.println("The strings are equal (ignoring case).");
} else {
System.out.println("The strings are not equal.");
}
}
}
📤 Output:
The strings are equal (ignoring case).
How It Works
Java compares characters using case-folding rules so H matches h, W matches w, and so on across the full string.
Example 2 — equalsIgnoreCase() vs equals()
Same pair of strings: one method ignores case, the other does not.
java
public class EqualsVsIgnoreCase {
public static void main(String[] args) {
String a = "Java";
String b = "java";
System.out.println("equals(): " + a.equals(b));
System.out.println("equalsIgnoreCase(): " + a.equalsIgnoreCase(b));
}
}
📤 Output:
equals(): false
equalsIgnoreCase(): true
How It Works
equals() sees different characters at the first letter. equalsIgnoreCase() treats them as the same word.
📈 Practical Patterns
Real input handling and related comparison methods.
Example 3 — Accepting User Commands
Treat yes, YES, and Yes as the same answer.
java
public class CommandParser {
public static void main(String[] args) {
String[] attempts = {"yes", "YES", "no", "No"};
for (String input : attempts) {
if (input.equalsIgnoreCase("yes")) {
System.out.println(input + " -> confirmed");
} else {
System.out.println(input + " -> declined");
}
}
}
}
📤 Output:
yes -> confirmed
YES -> confirmed
no -> declined
No -> declined
How It Works
Only capitalization differs for the first two inputs, so both match "yes". no and No do not.
Example 4 — Null-Safe Comparison
Put the known literal on the left when user input may be null.
java
public class NullSafeIgnoreCase {
public static void main(String[] args) {
String userInput = null;
System.out.println("Match quit? " + "quit".equalsIgnoreCase(userInput));
System.out.println("Null arg? " + "Java".equalsIgnoreCase(null));
}
}
📤 Output:
Match quit? false
Null arg? false
How It Works
Both calls return false safely. userInput.equalsIgnoreCase("quit") would throw NullPointerException.
Example 5 — equalsIgnoreCase() vs compareToIgnoreCase()
Use compareToIgnoreCase() when you need ordering; use equalsIgnoreCase() for a simple yes/no.
java
public class IgnoreCaseCompareMethods {
public static void main(String[] args) {
String s1 = "Apple";
String s2 = "apple";
System.out.println("equalsIgnoreCase: " + s1.equalsIgnoreCase(s2));
System.out.println("compareToIgnoreCase: " + s1.compareToIgnoreCase(s2));
}
}
📤 Output:
equalsIgnoreCase: true
compareToIgnoreCase: 0
How It Works
Both report equality. compareToIgnoreCase returns 0 when strings match ignoring case—useful for sorting lists alphabetically without case noise.
Applications
🚀 Common Use Cases
Menu and command parsing — accept exit, EXIT, or Exit as the same command.
Configuration flags — treat true, TRUE, and True as enabled.
File extension checks — when paired with case normalization or for ASCII extensions like .PDF vs .pdf.
Username or email local-part lookup — when policy says case should not distinguish users (domain rules vary).
Test assertions — verify output text without brittle exact-case expectations.
🧠 How equalsIgnoreCase() Works
1
You pass another String
The host string is compared against the argument.
Input
2
Null returns false
A null argument is handled safely and yields false.
Validate
3
Case-insensitive scan
Characters are matched using Unicode case-insensitive rules across the full length.
Compare
=
✅
boolean answer
true when text matches ignoring case; false otherwise.
Important
📝 Notes
equalsIgnoreCase() compares the entire string, not a substring—unlike contains().
It is more forgiving than equals() for letter case but still requires the same text (length and characters).
For passwords and security-sensitive values, prefer case-sensitive equals() unless policy says otherwise.
Unicode has locale-specific case rules; for most English tutorials and ASCII input, behavior matches intuition.
Do not use == for content comparison—use equals() or equalsIgnoreCase().
Performance
⚡ Optimization
equalsIgnoreCase() is optimized in the JDK and is preferable to manually calling toLowerCase() on both strings for most cases. If you compare the same pair repeatedly in a loop, cache the boolean result. When sorting many strings case-insensitively, use compareToIgnoreCase() or a Comparator instead of chaining multiple equalsIgnoreCase calls.
Wrap Up
Conclusion
The equalsIgnoreCase() method is the go-to choice when two strings should be treated as the same text regardless of capitalization. It keeps user-input handling simple and readable.
Remember when to prefer strict equals(), how null arguments behave, and how this method differs from compareToIgnoreCase(). With those details clear, case-insensitive comparisons become straightforward in real programs.
Use equalsIgnoreCase() when user capitalization may vary
Write "expected".equalsIgnoreCase(input) when input may be null
Use equals() when exact case must match (passwords, codes)
Use compareToIgnoreCase() for sorting, not just equality
Pick one comparison style per feature and stay consistent
❌ Don’t
Use equalsIgnoreCase() for security-sensitive exact tokens
Call it on a null string reference without guarding
Assume it finds substrings (use contains() for that)
Compare with == instead of content methods
Lowercase both strings manually when equalsIgnoreCase() reads clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about equalsIgnoreCase()
Use these points for case-insensitive string checks in Java.
5
Core concepts
🔑01
Ignore Case
A = a for compare.
Purpose
✅02
Returns boolean
true or false.
Return
⚖️03
vs equals()
Strict vs relaxed.
Compare
💬04
User Input
yes / YES / Yes.
Use case
📈05
vs compareTo
Equal vs order.
Related
❓ Frequently Asked Questions
equalsIgnoreCase(String anotherString) compares two strings for the same text while ignoring letter case. It returns true when both strings match if uppercase and lowercase are treated as equivalent.
equals() is case-sensitive: "Java".equals("java") is false. equalsIgnoreCase() ignores case: "Java".equalsIgnoreCase("java") is true. Use equalsIgnoreCase() when capitalization should not matter.
equalsIgnoreCase() returns true or false for equality. compareToIgnoreCase() returns an int (0 when equal, negative or positive for ordering). Use equalsIgnoreCase() for yes/no checks; compareToIgnoreCase() when you also need sort order.
It returns false without throwing an exception, just like equals(). Calling equalsIgnoreCase on a null string reference (null.equalsIgnoreCase("x")) throws NullPointerException.
Prefer equalsIgnoreCase() for simple comparisons. It is clearer and handles Unicode case rules correctly. Manual toLowerCase() on both sides works for basic ASCII but can be wrong for some locales unless you use toLowerCase(Locale).
Use it for command parsing (yes/YES), configuration flags, file type checks when case varies, username lookups where case should not matter, and any input validation where users may type in any capitalization.