Java null String equals result

后端 未结 8 741
星月不相逢
星月不相逢 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:43

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

提交回复
热议问题