Java字符串null检查!= null或! string .equals(null)?(复制)

时间:2022-07-19 16:12:48

This question already has an answer here:

这个问题已经有了答案:

What's the best way to check for not null values in java.

在java中检查非空值的最佳方法是什么?

String query ="abcd";

query != null vs !query.equals(null).

= null vs . query.equals(null)。

which is better?why?

哪个更好?为什么?

4 个解决方案

#1


24  

1st one is better (and the only option), because 2nd one will throw NPE, when your value is actually null. As simple as that.

第一个更好(也是唯一的选项),因为第二个会抛出NPE,当你的值实际上是空的。那么简单。

Try this out:

试试这个:

String str = null;
str.equals(null);  // will throw `NPE`.

So basically, the test which you wanted to perform itself triggers a NullPointerException in the 2nd case. So, it is no choice.

基本上,你想要执行的测试会在第二种情况下触发NullPointerException。所以,这是没有选择的。

#2


11  

!str.equals(null) will

! str.equals(null)

  1. always return false if it does not throw an exception, and
  2. 如果不抛出异常,则总是返回false
  3. throw an exception if str is null
  4. 如果str为空,抛出异常。

The point of a null check is to make sure the reference, in this code str, actually refers to an object. If it is null, you can't call any methods on it, including equals, without throwing a NullPointerException... which the point of null checks is to avoid happening.

空检查的要点是确保这个代码str中的引用实际上引用了一个对象。如果它是空的,那么就不能调用它的任何方法,包括equals,而不抛出NullPointerException…零检查的目的是避免发生这种情况。

So !str.equals(null) causes the very problem null checks are meant to prevent and doesn't do what you think at all. Never do this.

因此,string .equals(null)会导致非常问题的空检查是为了防止并且不做您认为的事情。从来没有这样做。

#3


2  

query != null better as !query.equals(null) will throw an exception when query is actually null. Also query != null is easily readable

当查询实际上为空时,查询!= null会抛出一个异常。也可以查询!= null很容易读。

#4


2  

query != null compares if the object is null. query.equals("something") compares the value inside of that object. string == "something". so in this case use query != null.

如果对象为空,那么查询!= null比较。equals(something)对该对象内部的值进行比较。字符串= =“东西”。在这种情况下,使用query != null。

#1


24  

1st one is better (and the only option), because 2nd one will throw NPE, when your value is actually null. As simple as that.

第一个更好(也是唯一的选项),因为第二个会抛出NPE,当你的值实际上是空的。那么简单。

Try this out:

试试这个:

String str = null;
str.equals(null);  // will throw `NPE`.

So basically, the test which you wanted to perform itself triggers a NullPointerException in the 2nd case. So, it is no choice.

基本上,你想要执行的测试会在第二种情况下触发NullPointerException。所以,这是没有选择的。

#2


11  

!str.equals(null) will

! str.equals(null)

  1. always return false if it does not throw an exception, and
  2. 如果不抛出异常,则总是返回false
  3. throw an exception if str is null
  4. 如果str为空,抛出异常。

The point of a null check is to make sure the reference, in this code str, actually refers to an object. If it is null, you can't call any methods on it, including equals, without throwing a NullPointerException... which the point of null checks is to avoid happening.

空检查的要点是确保这个代码str中的引用实际上引用了一个对象。如果它是空的,那么就不能调用它的任何方法,包括equals,而不抛出NullPointerException…零检查的目的是避免发生这种情况。

So !str.equals(null) causes the very problem null checks are meant to prevent and doesn't do what you think at all. Never do this.

因此,string .equals(null)会导致非常问题的空检查是为了防止并且不做您认为的事情。从来没有这样做。

#3


2  

query != null better as !query.equals(null) will throw an exception when query is actually null. Also query != null is easily readable

当查询实际上为空时,查询!= null会抛出一个异常。也可以查询!= null很容易读。

#4


2  

query != null compares if the object is null. query.equals("something") compares the value inside of that object. string == "something". so in this case use query != null.

如果对象为空,那么查询!= null比较。equals(something)对该对象内部的值进行比较。字符串= =“东西”。在这种情况下,使用query != null。