The String.equalsIgnoreCase() method in Java compares two strings character-by-character while ignoring differences in uppercase and lowercase letters, returning a boolean result.
Case-Insensitive Equality & Performance: Unlike case-sensitive equals(), this method treats "JAVA" and "java" as equal without creating temporary heap objects from toLowerCase() or toUpperCase(). For alphabetical sorting orders that ignore case, see compareToIgnoreCase(). To inspect character counts, explore length(), or sanitize surrounding spaces with trim().
The equalsIgnoreCase() method is defined directly in java.lang.String:
// Formal signature of String.equalsIgnoreCase() in java.lang.String
public boolean equalsIgnoreCase(String anotherString)
| Parameter / Element | Type | Description & Operational Rule |
|---|---|---|
anotherString |
String |
The target String to compare against. Can be null. |
| Return Value | boolean |
Returns true if characters match ignoring case; false if characters differ, lengths differ, or the argument is null. |
| Case Handling | Insensitive | Treats ASCII and Unicode upper/lower pairs as equivalent (e.g. 'A' == 'a'). |
When you call string1.equalsIgnoreCase(string2), Java checks whether both strings represent the same word regardless of letter casing:
String site1 = "plus2net";
String site2 = "Plus2net";
String site3 = "PLUS2NET";
String site4 = "2net";
System.out.println(site1.equalsIgnoreCase(site2)); // true
System.out.println(site1.equalsIgnoreCase(site3)); // true
System.out.println(site1.equalsIgnoreCase(site4)); // falseTerminal Output:
true
true
false
Understanding when to use case-sensitive versus case-insensitive comparisons prevents unexpected logic bugs:
| Feature | string1.equalsIgnoreCase(string2) |
string1.equals(string2) |
|---|---|---|
| Case Rule | Case-Insensitive ("A" == "a"). |
Case-Sensitive ("A" != "a"). |
| Parameter Type | String anotherString |
Object anObject |
| Primary Use Cases | Usernames, email domains, promo codes, commands. | Passwords, hash checksums, case-exact tokens. |
String input = "Java";
String target = "java";
// equals(): Strict character-code comparison
System.out.println("equals(): " + input.equals(target)); // false
// equalsIgnoreCase(): Case-insensitive comparison
System.out.println("equalsIgnoreCase(): " + input.equalsIgnoreCase(target)); // trueTerminal Output:
equals(): false
equalsIgnoreCase(): true
A common beginner mistake is writing s1.toLowerCase().equals(s2.toLowerCase()). Calling toLowerCase() allocates two brand-new temporary String objects on the heap, triggering unnecessary memory churn and garbage collection. equalsIgnoreCase() operates in-place with zero memory allocation:
String a = "HELLO";
String b = "hello";
// SLOW & MEMORY INEFFICIENT: Creates 2 temporary heap String objects
boolean slowCheck = a.toLowerCase().equals(b.toLowerCase());
// FAST & OPTIMAL: In-place comparison with zero extra heap allocations
boolean fastCheck = a.equalsIgnoreCase(b);
System.out.println("toLowerCase().equals(): " + slowCheck);
System.out.println("equalsIgnoreCase(): " + fastCheck);Terminal Output:
toLowerCase().equals(): true
equalsIgnoreCase(): true
Invoking .equalsIgnoreCase() on a variable storing null causes a NullPointerException. To write defensive code, place the known non-null constant on the left (Yoda condition) or use explicit null guards:
String userInput = null;
// RISKY: Throws NullPointerException when invoked on null
try {
System.out.println(userInput.equalsIgnoreCase("admin"));
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException!");
}
// SAFE APPROACH 1: "Yoda condition" (constant literal first)
System.out.println("admin".equalsIgnoreCase(userInput)); // false (no crash)
// SAFE APPROACH 2: Null check guard
boolean isValid = (userInput != null) && userInput.equalsIgnoreCase("admin");
System.out.println("Null guard result: " + isValid); // falseTerminal Output:
Caught NullPointerException!
false
Null guard result: false
While equalsIgnoreCase() tests equality returning a boolean, compareToIgnoreCase() returns a signed integer representing alphabetical ordering:
String s1 = "JAVA";
String s2 = "java";
String s3 = "KOTLIN";
// equalsIgnoreCase(): returns boolean identity
System.out.println("s1.equalsIgnoreCase(s2): " + s1.equalsIgnoreCase(s2)); // true
System.out.println("s1.equalsIgnoreCase(s3): " + s1.equalsIgnoreCase(s3)); // false
// compareToIgnoreCase(): returns integer distance for alphabetical sorting
System.out.println("s1.compareToIgnoreCase(s2): " + s1.compareToIgnoreCase(s2)); // 0
System.out.println("s1.compareToIgnoreCase(s3): " + s1.compareToIgnoreCase(s3)); // -1 ('j' < 'k')Terminal Output:
s1.equalsIgnoreCase(s2): true
s1.equalsIgnoreCase(s3): false
s1.compareToIgnoreCase(s2): 0
s1.compareToIgnoreCase(s3): -1
In console and web applications, user commands must be recognized regardless of whether the user enters lowercase, uppercase, or mixed casing:
public class CommandParser {
public static boolean isExitCommand(String cmd) {
if (cmd == null) return false;
String clean = cmd.trim(); // strip leading/trailing whitespace
return "exit".equalsIgnoreCase(clean) || "quit".equalsIgnoreCase(clean);
}
public static void main(String[] args) {
System.out.println("Command 'QUIT': " + isExitCommand("QUIT")); // true
System.out.println("Command ' Exit ': " + isExitCommand(" Exit ")); // true
System.out.println("Command 'help': " + isExitCommand("help")); // false
}
}Terminal Output:
Command 'QUIT': true
Command ' Exit ': true
Command 'help': false
" admin ".equalsIgnoreCase("admin") returns false. equalsIgnoreCase() ignores character casing, but it does not ignore spaces. Always combine with .trim() when validating user input.
Passwords and API secret tokens are strictly case-sensitive. Comparing passwords with equalsIgnoreCase() degrades system security by collapsing distinct security permutations into identical credentials.
You can copy this self-contained class into any editor or IDE to compile and verify all case-insensitive comparisons:
public class IgnoreCaseFullDemo {
public static void main(String[] args) {
String promoInput = "SAVE50";
String promoStored = "save50";
// 1. Core comparison
if (promoInput.equalsIgnoreCase(promoStored)) {
System.out.println("Check 1 Passed: Promo code matched regardless of case.");
}
// 2. Safe literal-first comparison with null input
String unverified = null;
if (!"YES".equalsIgnoreCase(unverified)) {
System.out.println("Check 2 Passed: Null input safely rejected without exception.");
}
// 3. Alphabetical ordering parity
if (promoInput.compareToIgnoreCase(promoStored) == 0) {
System.out.println("Check 3 Passed: compareToIgnoreCase returned 0 for matching codes.");
}
System.out.println("All String.equalsIgnoreCase() unit tests completed successfully!");
}
}Terminal Output:
Check 1 Passed: Promo code matched regardless of case.
Check 2 Passed: Null input safely rejected without exception.
Check 3 Passed: compareToIgnoreCase returned 0 for matching codes.
All String.equalsIgnoreCase() unit tests completed successfully!
Passing null as the argument safely returns false without throwing an exception: "test".equalsIgnoreCase(null) == false.
For strings with identical casing, equalsIgnoreCase() exits immediately like equals(). For mismatched cases, it performs small character upper/lower checks, but it is vastly faster than calling toLowerCase().equals().
No. equalsIgnoreCase() requires a String parameter. Convert the StringBuilder first using sb.toString().
No. equalsIgnoreCase() uses Unicode standard case foldings without being affected by the host platform's default locale.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.