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 的一部分:http://findbugs.sourceforge.net/manual/annotations.html
Update: The two annotations above are part of JSR-305/FindBugs: http://findbugs.sourceforge.net/manual/annotations.html
推荐答案
我认为从您添加的链接中可以很清楚地看出:如果您使用 @CheckForNull
并且使用该值的代码没有检查 null
,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
.
在实践中,此注释仅用于覆盖总体NonNull
注释.
In practice this annotation is useful only for overriding an overarching
NonNull
annotation.
在必须始终检查值的情况下使用 @CheckForNull
.使用 @Nullable
,其中 null
可能没问题.
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屋!