[英]Best way to check for null values in Java?
Before calling a function of an object, I need to check if the object is null, to avoid throwing a NullPointerException
.在调用 object 的 function 之前,我需要检查 object 是否为NullPointerException
。
What is the best way to go about this? go 关于这个的最佳方法是什么? I've considered these methods.我考虑过这些方法。
Which one is the best programming practice for Java? Java的最佳编程实践是哪一个?
// Method 1
if (foo != null) {
if (foo.bar()) {
etc...
}
}
// Method 2
if (foo != null ? foo.bar() : false) {
etc...
}
// Method 3
try {
if (foo.bar()) {
etc...
}
} catch (NullPointerException e) {
}
// Method 4 -- Would this work, or would it still call foo.bar()?
if (foo != null && foo.bar()) {
etc...
}
Method 4 is best.方法4最好。
if(foo != null && foo.bar()) {
someStuff();
}
will use short-circuit evaluation , meaning it ends if the first condition of a logical AND
is false.将使用短路评估,这意味着如果logical AND
的第一个条件为假,则结束。
The last and the best one.最后一个也是最好的。 ie LOGICAL AND即逻辑与
if (foo != null && foo.bar()) {
etc...
}
Because in logical &&
因为在逻辑&&
it is not necessary to know what the right hand side is, the result must be false没有必要知道右手边是什么,结果一定是假的
Prefer to read : Java logical operator short-circuiting喜欢阅读: Java逻辑运算符短路
In Java 8, the best way to check for null is: 在Java 8中,检查null的最佳方法是:
Objects.isNull(obj) //returns true if the object is null
Objects.nonNull(obj) //returns true if object is not-null
if(Objects.nonNull(foo) && foo.something()) // Uses short-circuit as well. No Null-pointer Exceptions are thrown.
Apart from this... You can also go with Guava's Optional Class 除此之外......您还可以选择Guava的Optional Class
Reduces the typological errors like (obj=null)
which always returns true
as discussed in previous answers. 减少类似于(obj=null)
的类型错误,它总是返回true
如前面的答案所述。
NullPointerException
.不要捕获NullPointerException
。 That is a bad practice.这是一种不好的做法。 It is better to ensure that the value is not null.最好确保该值不为空。Method 4 is far and away the best as it clearly indicates what will happen and uses the minimum of code.方法 4 无疑是最好的,因为它清楚地表明会发生什么并且使用最少的代码。
Method 3 is just wrong on every level.方法 3 在各个层面上都是错误的。 You know the item may be null so it's not an exceptional situation it's something you should check for.您知道该项目可能为空,因此这不是特殊情况,您应该检查一下。
Method 2 is just making it more complicated than it needs to be.方法 2 只是使它比需要的更复杂。
Method 1 is just method 4 with an extra line of code.方法 1 只是方法 4,多了一行代码。
In Java 7 , you can use Objects.requireNonNull()
.在Java 7 中,您可以使用Objects.requireNonNull()
。 Add an import of Objects
class from java.util
.从java.util
添加Objects
类的导入。
public class FooClass {
//...
public void acceptFoo(Foo obj) {
//If obj is null, NPE is thrown
Objects.requireNonNull(obj).bar(); //or better requireNonNull(obj, "obj is null");
}
//...
}
I would say method 4 is the most general idiom from the code that I've looked at.我会说方法 4 是我看过的代码中最通用的习惯用法。 But this always feels a bit smelly to me.但这对我来说总是有点臭。 It assumes foo == null is the same as foo.bar() == false.它假定 foo == null 与 foo.bar() == false 相同。
That doesn't always feel right to me.这对我来说并不总是正确的。
Method 4 is my preferred method.方法 4 是我的首选方法。 The short circuit of the && operator makes the code the most readable. && 运算符的短路使代码最具可读性。 Method 3, Catching NullPointerException, is frowned upon most of the time when a simple null check would suffice.方法 3,Catching NullPointerException,在大多数情况下是不赞成的,因为简单的 null 检查就足够了。
If you control the API being called, consider using Guava's Optional class如果你控制被调用的 API,可以考虑使用Guava 的 Optional 类
More info here .更多信息在这里。 Change your method to return an Optional<Boolean>
instead of a Boolean
.更改您的方法以返回Optional<Boolean>
而不是Boolean
。
This informs the calling code that it must account for the possibility of null, by calling one of the handy methods in Optional
通过调用Optional
一种方便的方法,这会通知调用代码它必须考虑 null 的可能性
As others have said #4 is the best method when not using a library method.正如其他人所说,#4 是不使用库方法时的最佳方法。 However you should always put null on the left side of the comparison to ensure you don't accidentally assign null to foo in case of typo.但是,您应该始终将 null 放在比较的左侧,以确保在出现拼写错误时不会意外地将 null 分配给 foo。 In that case the compiler will catch the mistake.在这种情况下,编译器会发现错误。
// You meant to do this
if(foo != null){
// But you made a typo like this which will always evaluate to true
if(foo = null)
// Do the comparison in this way
if(null != foo)
// So if you make the mistake in this way the compiler will catch it
if(null = foo){
// obviously the typo is less obvious when doing an equality comparison but it's a good habit either way
if(foo == null){
if(foo = null){
您还可以使用StringUtils.isNoneEmpty("")
来检查是否为 null 或为空。
If at all you going to check with double equal "==" then check null with object ref like如果你打算用双等号“==”检查,那么用对象引用检查 null 就像
if(null == obj)
instead of代替
if(obj == null)
because if you mistype single equal if(obj = null) it will return true (assigning object returns success (which is 'true' in value).因为如果你输入错误 single equal if(obj = null) 它将返回 true (分配对象返回成功(其值为 'true' )。
if you do not have an access to the commons apache library, the following probably will work ok如果您无权访问 commons apache 库,则以下内容可能会正常工作
if(null != foo && foo.bar()) {
//do something
}
Your last proposal is the best.你最后的提议是最好的。
if (foo != null && foo.bar()) {
etc...
}
Because:因为:
We can use Object.requireNonNull static method of Object class.我们可以使用 Object 类的 Object.requireNonNull 静态方法。 Implementation is below实现如下
public void someMethod(SomeClass obj) {
Objects.requireNonNull(obj, "Validation error, obj cannot be null");
}
简单的一行代码来检查 null :
namVar == null ? codTdoForNul() : codTdoForFul();
For java 11+ you can use Objects.nonNull(Object obj)
对于 java 11+,您可以使用Objects.nonNull(Object obj)
if(nonNull(foo)){
//
}
public <T, U> U defaultGet(T supplier, Function<T, U> mapper, U defaultValue) {
return Optional.ofNullable(supplier).map(mapper).orElse(defaultValue);
}
You can create this function if you prefer function programming如果您喜欢函数编程,可以创建此函数
If that object is not null we are going to do the following things.如果该对象不为空,我们将执行以下操作。
a.一种。 We can mutate the object (I)我们可以改变对象(I)
b.湾We can return something(O) as output instead of mutating the object (I)我们可以返回 something(O) 作为输出而不是改变对象 (I)
c. C。 we can do both我们可以同时做
In this case, We need to pass a function which needs to take the input param(I) which is our object If we take it like that, then we can mutate that object if we want.在这种情况下,我们需要传递一个函数,该函数需要接受作为我们对象的输入 param(I) 如果我们这样接受,那么我们可以根据需要改变该对象。 and also that function may be something (O).而且该功能可能是(O)。
If object is null then we are going to do the following things如果 object 为 null 那么我们将做以下事情
a.一种。 We may throw an exception in a customized way我们可能会以自定义的方式抛出异常
b.湾We may return something.我们可能会返回一些东西。
In this case, the object is null so we need to supply the value or we may need to throw an exception.在这种情况下,对象为 null,因此我们需要提供该值,否则我们可能需要抛出异常。
I take two examples.我举两个例子。
public String trimValue(String s){
return s == null ? null : s.trim();
}
public void setTeacherAge(Teacher teacher, int age){
if (teacher != null){
teacher.setAge(age);
} else{
throw new RuntimeException("teacher is null")
}
}
With my Explanation, I have created a generic method that takes the value(value may be null), a function that will execute if the object is not null and another supplier function that will execute if the object is null.在我的解释中,我创建了一个接受值(值可能为空)的通用方法,一个在对象不为空时将执行的函数,以及在对象为空时将执行的另一个供应商函数。
public <I, O> O setNullCheckExecutor(I value, Function<I, O> nonNullExecutor, Supplier<O> nullExecutor) {
return value != null ? nonNullExecutor.apply(value) : nullExecutor.get();
}
So after having this generic function, we can do as follow for the example methods 1.所以有了这个通用函数后,我们可以对示例方法1进行如下操作。
//To Trim a value
String trimmedValue = setNullCheckExecutor(value, String::trim, () -> null);
Here, the nonNullExecutor Function is trim the value (Method Reference is used).这里, nonNullExecutor 函数是修剪值(使用方法参考)。 nullExecutorFunction is will return null since It is an identity function. nullExecutorFunction 将返回 null,因为它是一个标识函数。
2. 2.
// mutate the object if not null otherwise throw a custom message runtime exception instead of NullPointerException
setNullCheckExecutor(teacher, teacher -> {
teacher.setAge(19);
return null;
}, () -> {
throw new RuntimeException("Teacher is null");
});
Allot of times I look for null when processing a function -在处理 function 时,我经常寻找 null -
public static void doSomething(Object nullOrNestedObject) {
if (nullOrNestedObject == null || nullOrNestedObject.getNestedObject()) {
log.warn("Invalid argument !" );
return;
// Or throw an exception
// throw new IllegalArgumentException("Invalid argument!");
}
nullOrNestedObject.getNestedObject().process()
... // Do other function stuff
}
That way if it is null it just stops execution early, and you don't have to nest all of your logic in an if.这样,如果它是 null 它只会提前停止执行,并且您不必将所有逻辑嵌套在 if 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.