javax.annotation中:@Nullable VS @CheckForNull [英] javax.annotation: @Nullable vs @CheckForNull
问题描述
两者有什么区别?似乎都意味着该值可能为空,并应与相应即检查空处理。
What is the difference between the two? Both seem to mean that the value may be null and should be dealt with accordingly i.e. checked for null.
更新:
上述两个注解是JSR-305 / FindBugs的一部分:
<一href=\"http://findbugs.sourceforge.net/manual/annotations.html\">http://findbugs.sourceforge.net/manual/annotations.html
推荐答案
我觉得这是pretty从您添加的链接清楚:如果你使用 @CheckForNull
和使用该值code不检查空
,FindBugs的将其显示为错误。
I think it is pretty clear from the link you added: if you use @CheckForNull
and the code that uses the value does not check for null
, FindBugs will show it as an error.
FindBugs的会忽略 @Nullable
。
FindBugs will ignore @Nullable
.
在实践中,这批注仅用于覆盖一个总体是非常有用的非空
注释。
In practice this annotation is useful only for overriding an overarching
NonNull
annotation.
使用 @CheckForNull
的情况下,当值必须始终进行检查。使用 @Nullable
,其中空
可能确定。
Use @CheckForNull
in the cases when the value must always be checked. Use @Nullable
where null
might be OK.
编辑:看来, @CheckForNull
不是此刻很好的支持,所以我建议避免它,并使用 @NonNull
(另见 @NotNull Java注释我应该使用哪个?)。
另一个想法是接触与FindBugs的开发人员能够直接,询问文档中的不一致的意见。
it seems that @CheckForNull
is not well supported at the moment, so I suggest avoiding it and using @NonNull
(also see Which @NotNull Java annotation should I use?).
Another idea would be to get in touch directly with the FindBugs developers, and ask their opinion about the inconsistency in the documentation.
这篇关于javax.annotation中:@Nullable VS @CheckForNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!