Let's assume you are right and see what's going to happen when we call equalsIgnoreCase(null):

  1. this == anotherString is false;
  2. anotherString.value.length == value.length - We are getting a NPE on anotherString.value.

Therefore, anotherString != null is necessary and crucial here.


this != anotherString already indicates that anotherString is not null.

No, it doesn't. It can only state whether this and anotherString not equal.

For instance, both this != null and this != "test" return true.

Answer from Andrew on Stack Overflow
Top answer
1 of 16
187

string == null compares if the object is null. string.equals("foo") compares the value inside of that object. string == "foo" doesn't always work, because you're trying to see if the objects are the same, not the values they represent.


Longer answer:

If you try this, it won't work, as you've found:

String foo = null;
if (foo.equals(null)) {
    // That fails every time. 
}

The reason is that foo is null, so it doesn't know what .equals is; there's no object there for .equals to be called from.

What you probably wanted was:

String foo = null;
if (foo == null) {
    // That will work.
}

The typical way to guard yourself against a null when dealing with Strings is:

String foo = null;
String bar = "Some string";
...
if (foo != null && foo.equals(bar)) {
    // Do something here.
}

That way, if foo was null, it doesn't evaluate the second half of the conditional, and things are all right.

The easy way, if you're using a String literal (instead of a variable), is:

String foo = null;
...
if ("some String".equals(foo)) {
    // Do something here.
}

If you want to work around that, Apache Commons has a class - StringUtils - that provides null-safe String operations.

if (StringUtils.equals(foo, bar)) {
    // Do something here.
}

Another response was joking, and said you should do this:

boolean isNull = false;
try {
    stringname.equalsIgnoreCase(null);
} catch (NullPointerException npe) {
    isNull = true;
}

Please don't do that. You should only throw exceptions for errors that are exceptional; if you're expecting a null, you should check for it ahead of time, and not let it throw the exception.

In my head, there are two reasons for this. First, exceptions are slow; checking against null is fast, but when the JVM throws an exception, it takes a lot of time. Second, the code is much easier to read and maintain if you just check for the null pointer ahead of time.

2 of 16
33
s == null

won't work?

🌐
Coderanch
coderanch.com › t › 545457 › java › comparing-values-equalsIgnoreCase-equals-NULL
comparing two values with equalsIgnoreCase and equals with NULL as first value. (Java in General forum at Coderanch)
Hello Davies, You should not call equals() method using the reference which points to "null". In the first case yo will get NullPointerException obviously. but the second approach is correct. see the java SE API.
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - The method returns true if any of the other given Strings match against the first String case sensitively. ... assertThat(StringUtils.equalsAny(null, null, null)) .isTrue(); assertThat(StringUtils.equalsAny("equals any", "equals any", "any")) ...
🌐
Fantom
fantom.org › forum › topic › 2347
Should Str.equalsIgnoreCase(Str str) accept null? – Fantom
By convention you should really be using == to test equality and it has special handling for nullable on either side. You can't really do that when calling an OO method on a string. So I think its best left as non-nullable · FWIW Java equalsIgnoreCase accepts returns false for null on the RHS.
🌐
Scaler
scaler.com › home › topics › equalsignorecase() in java
equalsIgnoreCase() in Java - Scaler Topics
April 26, 2022 - Let's consider a code snippet (string1.equalsIgnoreCase(string2)) to understand the exceptions. The following cases may arise: If the input string, i.e., string2 is null, the method will return false, it doesn’t throw any exceptions.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java NullPointerException Avoidance and Enhancement Tactics - Java Code Geeks
March 1, 2021 - Although it was specifically String.equals(Object) demonstrated above, this tactic applies to instances of any class as long as the class’s .equals(Object) method can gracefully handle a supplied null (and I cannot recall ever encountering one that didn’t handle null). Case Insensitive Comparison of Strings Safely with Known Non-null String on LHS of .equals(Object)
🌐
DEV Community
dev.to › satyam_gupta_0d1ff2152dcc › java-equalsignorecase-explained-your-guide-to-case-insensitive-string-comparison-37g1
Java equalsIgnoreCase() Explained: Your Guide to Case-Insensitive String Comparison - DEV Community
November 4, 2025 - As shown in the best practices, call the method on the string literal or the one you are sure is not null. java // Safe way if ("knownString".equalsIgnoreCase(possibleNullString)) { // This is safe even if possibleNullString is null } // OR, ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-equalsignorecase-method-with-examples
Java String equalsIgnoreCase() Method - GeeksforGeeks
December 23, 2024 - In Java, equalsIgnoreCase() method ... string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring case, else false....
🌐
Tabnine
tabnine.com › home page › code › java › org.apache.commons.lang.stringutils
org.apache.commons.lang.StringUtils.equalsIgnoreCase java code examples | Tabnine
String oldVal = oldP == null ? null : oldP.get(StatsSetupConst.COLUMN_STATS_ACCURATE); String newVal = newP == null ? null : newP.get(StatsSetupConst.COLUMN_STATS_ACCURATE); // We don't need txn context is that stats state is not being changed. if (StringUtils.isEmpty(oldVal) && StringUtils.isEmpty(newVal)) return null; if (StringUtils.equalsIgnoreCase(oldVal, newVal)) { if (!isColStatsChange) return null; // No change in col stats or parameters => assume no change.
🌐
Baeldung
baeldung.com › home › java › java string › java string equalsignorecase()
Java String equalsIgnoreCase() | Baeldung
January 8, 2024 - String lower = "equals ignore case"; String UPPER = "EQUALS IGNORE CASE"; assertThat(StringUtils.equalsIgnoreCase(lower, UPPER)).isTrue(); assertThat(StringUtils.equalsIgnoreCase(lower, null)).isFalse();
🌐
CodeGym
codegym.cc › java blog › strings in java › string equalsignorecase() method in java
String equalsIgnoreCase() Method in Java
February 13, 2025 - It does not handle null values, meaning that calling this method on a null string will result in a NullPointerException. It also does not ignore whitespace, so "hello " and "hello" will still be considered different.
🌐
Tabnine
tabnine.com › home page › code › java › java.lang.string
Java Examples & Tutorials of String.equalsIgnoreCase (java.lang) | Tabnine
/** * Return {@code true} if the ... * @see javax.servlet.ServletRequest#isSecure() */ @Override public boolean isSecure() { return (this.secure || HTTPS.equalsIgnoreCase(this.scheme)); } ... private boolean isDotDot(String input) { return input.equals("..") || input.equalsIgnoreCase("..") || input.equalsIgnoreCase("..") || input.equalsIgnoreCase(".."); } ... private static @Nullable String ...
🌐
How to do in Java
howtodoinjava.com › home › string › java string equalsignorecase()
Java String equalsIgnoreCase() with Examples - HowToDoInJava
January 6, 2023 - The syntax to use the equalsIgnoreCase() API is as follows: boolean isEqual = thisString.equalsIgnoreCase( anotherString ); Note that if we pass null as the method argument, the comparison result will be false.
🌐
GeeksforGeeks
geeksforgeeks.org › equalsignorecase-in-java
equalsIgnoreCase() in Java - GeeksforGeeks
December 4, 2018 - This method returns true if the argument is not null and it represents an equivalent String ignoring case, else false. ... Input : str1 = "pAwAn"; str2 = "PAWan" str2.equalsIgnoreCase(str1); Output :true Input : str1 = "powAn"; str2 = "PAWan" ...
🌐
W3Resource
w3resource.com › java-tutorial › string › string_equalsignorecase.php
Java String: equalsIgnoreCase Method - w3resource
Return Value: true if the argument is not null and it represents an equivalent String ignoring case; false otherwise. ... The following example shows the usage of java String() method. public class Example { public static void main(String[] ...