Java null String equals result

后端 未结 8 715
星月不相逢
星月不相逢 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).

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

    To prevent NPE while comparing Strings if at least one of them can be null, use StringUtils.equals method which is null-safe.

    0 讨论(0)
提交回复
热议问题