Java null String equals result

103,561

Solution 1

You cannot use the dereference (dot, '.') operator to access instance variables or call methods on an instance if that instance is null. Doing so will yield a NullPointerException.

It is common practice to use something you know to be non-null for string comparison. For example, "something".equals(stringThatMayBeNull).

Solution 2

Use Objects.equals() to compare strings, or any other objects if you're using JDK 7 or later. It will handle nulls without throwing exceptions. See more here: how-do-i-compare-strings-in-java

And if you're not running JDK 7 or later you can copy the equals method from Objects like this:

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

Solution 3

Indeed, you cannot use the dot operator on a null variable to call a non static method.

Despite this, all depends on overriding the equals() method of the Object class. In the case of the String class, is:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

If you pass null as parameter, both "if" will fail, returning false;

An alternative for your case is to build a method for your requirements:

public static boolean myEquals(String s1, String s2){
    if(s1 == null)
        return s2 == null;
    return s1.equals(s2);
}

Solution 4

That piece of code will throw a NullPointerException whenever string1 is null and you invoke equals on it, as is the case when a method is implicitly invoked on any null object.

To check if a string is null, use == rather than equals.

Although result1 and result3 will not be set due to NullPointerExceptions, result2 would be false (if you ran it outside the context of the other results).

Solution 5

You will get a NullPointerException in case 1 and case 3.

You cannot call any methods (like equals()) on a null object.

Share:
103,561
user1097772
Author by

user1097772

"I know one thing, that I know nothing" Socrates

Updated on February 11, 2022

Comments

  • user1097772
    user1097772 about 2 years

    Please help me how does the string.equals in java work with null value? Is there some problem with exceptions? Three cases:

    boolean result1,result2, result3;
    
        //1st case
        String string1 = null;
        String string2 = null;
        result = string1.equals(string2);
        //2nd case
        String string1 = "something";
        String string2 = null;
        result2 = string1.equals(string2);
        //3rd case 
        String string1 = null;
        String string2 = "something";
        result3 = string1.equals(string2);
    

    What the values of results are? I expect this values:

    result1 is true;
    result2 is false;
    result3 is false;

  • Steve Kuo
    Steve Kuo almost 12 years
    Not completely true. You can invoke a static method on a null reference.
  • user1097772
    user1097772 almost 12 years
    Ok thats what I needed to know, thanks. I'll use empty strings "" instead of null string :-)
  • FThompson
    FThompson almost 12 years
    @user1097772 Empty strings are not equal to null.
  • user1097772
    user1097772 almost 12 years
    @Vulcan Yes I know that. I mean that I'll not use the null strings but empty strings. The values of string1 and string2 in my example are results from my parser ... I'll ensure not null strings and I'll have no problem. Empty values will be represented by empty string instead of null string.
  • zacronos
    zacronos over 5 years
    While the example of "a string literal".equals(someStringVariable) in the accepted answer is to be commended, that doesn't help OP figure out how to handle the case where both strings are null (despite that being the first case OP presented). This is the best answer, as it handles all 3 cases without using custom code or a 3rd party library.