为什么 Android Studio 有这个 @Nullable 东西?
Why Android Studio has this @Nullable thing?
我只是对 Android Studio 向我展示的这个奇怪的东西感到好奇。当这个类似工具提示的东西弹出时,我正要使用 addView
方法:
我很困惑,因为参数上的 @Nullable
注释。 View
可以为空,因为它是引用类型。但为什么要显式 @Nullable
注释?
当我进入 addView
方法的定义时,我没有看到注释!那我就更糊涂了
所以我想问一下为什么Android工作室有这个奇怪的东西。
View
can be null because it is a reference type. But why an explicit
@Nullable
annotation?
在技术意义上,是的,View 是参考,因此 null
是值之一。同样,其中一个重载中的 width
和 height
参数属于 int
类型,因此从技术上讲,您可以传递 0
、2147483647
或 -1
值作为高度,例如,但它可能没有任何意义。同样,将 null
作为值传递,无论语法上如何有效,都可能有意义也可能没有意义。某些函数在传递 null
值时抛出 NullPointerException
。 @Nullable
注释意味着 null
是可接受的值,函数将处理:
Denotes that a parameter, field or method return value can be null.
When decorating a method call parameter, this denotes that the
parameter can legitimately be null and the method will gracefully deal
with it. Typically used on optional parameters.
When decorating a method, this denotes the method might legitimately
return null.
This is a marker annotation and it has no specific attributes.
我只是对 Android Studio 向我展示的这个奇怪的东西感到好奇。当这个类似工具提示的东西弹出时,我正要使用 addView
方法:
我很困惑,因为参数上的 @Nullable
注释。 View
可以为空,因为它是引用类型。但为什么要显式 @Nullable
注释?
当我进入 addView
方法的定义时,我没有看到注释!那我就更糊涂了
所以我想问一下为什么Android工作室有这个奇怪的东西。
View
can be null because it is a reference type. But why an explicit@Nullable
annotation?
在技术意义上,是的,View 是参考,因此 null
是值之一。同样,其中一个重载中的 width
和 height
参数属于 int
类型,因此从技术上讲,您可以传递 0
、2147483647
或 -1
值作为高度,例如,但它可能没有任何意义。同样,将 null
作为值传递,无论语法上如何有效,都可能有意义也可能没有意义。某些函数在传递 null
值时抛出 NullPointerException
。 @Nullable
注释意味着 null
是可接受的值,函数将处理:
Denotes that a parameter, field or method return value can be null.
When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.
When decorating a method, this denotes the method might legitimately return null.
This is a marker annotation and it has no specific attributes.