Java null String equals result

后端 未结 8 714
星月不相逢
星月不相逢 2020-12-16 15:11

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;         


        
相关标签:
8条回答
  • 2020-12-16 15:26

    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);
    }
    
    0 讨论(0)
  • 2020-12-16 15:29

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

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

    0 讨论(0)
  • 2020-12-16 15:31

    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

    0 讨论(0)
  • 2020-12-16 15:40

    Our most common use-case of this type of thing is when we have a database field that contains "Y" or "N" to represent a Boolean (it's an old system, don't ask).

    Thus, we do this:

    if ("Y".equals(stringObjectThatMayBeNull) ? result : otherResult);

    Instead of this:

    if (stringObjectThatMayBeNull.equals("Y") ? result : otherResult);

    ... which avoids a NullPointerException when executing the .equals method.

    0 讨论(0)
  • 2020-12-16 15:42

    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).

    0 讨论(0)
  • 2020-12-16 15:42

    We cannot use dot operator with null since doing so will give NullPointerException. Therefore we can take advantage of try..catch block in our program. This is a very crude way of solving your problem, but you will get desired output.

    try {
       result = string1.equals(string2);
    } catch (NullPointerException ex) {
       result = string2 == null; //This code will be executed only when string 1 is null
    }
    
    0 讨论(0)
提交回复
热议问题