空检查vs可选存在检查 [英] Null check vs Optional is present check
问题描述
有人可以解释可选
如何帮助我们避免 NullPointerException
?
Can someone explain how Optional
helps us avoid NullPointerException
?
Optional<String> op = someFunc()
if(op.isPresent()) {
op.get();
}
String possibleNull = op.get();
不是这个代码容易出现 NullPointerException
太?如果是这样,那么为什么这个代码优先于
Isn't this code prone to NullPointerException
too? If so, then why is this code preferred over
String op = someFunc()
if(op != null) {
op.get();
}
String possibleNull = op;
可选的
它帮助我们了解一个函数实际上是否有返回值。
What possible benefit does Optional
provide other than the fact that it helps us in knowing whether a function actually had a return value or not
推荐答案
假设您想获得一个字符串由函数返回,将其转换为大写,然后将其打印出来。如果你有:
Let's say you want to get a string returned by a function, convert it to upper case, and then print it out. If you have:
String someFunc() { ... }
您可能会想写:
You might be tempted to write:
System.out.println(someFunc().toUpperCase());
当然,这会抛出 NullPointerException
if if someFunc
返回 null
。相反,假设我们有这个:
Of course, this throws NullPointerException
if someFunc
returns null
. Instead, suppose we have this:
Optional<String> someFunc() { ... }
然后
Then
System.out.println(someFunc().toUpperCase());
将不起作用,因为可选
doesn没有 toUpperCase
方法。在这一点上 - 希望 - 你会遇到一个可选
,这应该让你考虑可选
空着。这有助于避免NPE,但可能只是有点。
won't work, since Optional
doesn't have a toUpperCase
method. At this point -- hopefully -- you'll be confronted with an Optional
, which should make you think about the case of the Optional
being empty. This helps avoid NPEs, but probably only somewhat.
现在您可能会专注于如何从可选
,你可能会忘记空的情况。嗯,有一个 get
方法:
Now you might be focusing on how to get the value out of the Optional
, and you might forget about the empty case. Ah, there's a get
method:
System.out.println(someFunc().get().toUpperCase());
这会带来与NPE相同的问题,除了例外是 NoSuchElementException改为
。所以如果你在可选的
上盲目地调用获取
,它确实和调用而不检查它是否为空。
This brings back the same problem as NPE, except that the exception is NoSuchElementException
instead. So if you blindly call get
on an Optional
, it really is pretty much the same thing as calling a method on a reference without checking whether it's null.
(因此, Brian Goetz 认为 Optional.get
是Java 8中最大的错误。请参阅他对Angelika Langer的采访 。不知道它是最大的,但它是一个错误,人们不希望 get
抛出异常。)
(For this reason, Brian Goetz considers Optional.get
to be the biggest mistake in Java 8. See his interview with Angelika Langer JAX 2015 Fragen und Antworten zu Java 8 at about 16 minutes in. I'm not sure it's the biggest, but it is a mistake. People just don't expect get
to throw an exception.)
如果您努力检查空引用或空白选项,那么
If you're diligent about checking for null references or empty optionals, then
Optional<String> os = someFunc();
if (os.isPresent()) {
System.out.println(os.get().toUpperCase());
}
几乎没有旧的
is hardly any better than the old
String s = someFunc();
if (s != null) {
System.out.println(s.toUpperCase());
}
真实优势>可选
是它是一个库类,它有一个相当丰富的API来以安全的方式处理空白的案例。通过将一些方法调用链接到返回可选$ c>的方法来处理可能包含在
Optional
中的值通常是可能的$ c>首先。例如,我们可以重写上面的示例,如下所示:
The real advantage of Optional
is that it's a library class that has a fairly rich API for dealing with the empty case in a safe way. It's often possible to process the value that might be contained within an Optional
by chaining a couple method calls to the method that returned the Optional
in the first place. For example, we could rewrite the sample above as follows:
someFunc().map(String::toUpperCase)
.ifPresent(System.out::println);
这篇关于空检查vs可选存在检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!