I sometimes see statements like somevariable.value?.add()
What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
I sometimes see statements like somevariable.value?.add()
What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
Kotlin treats null
as something more than the source of null-pointer exceptions.
在您的代码片段中,somevariable.value
是"可空类型",例如MutableList?
或Axolotl?
.MutableList
不能是null
,但MutableList?
可能是null
.
通常,要在对象上调用函数,需要使用.
.
对变量、参数或属性调用函数的一个选项
null
, your function call is ignored, and null
is the
resultnull
,则函数调用将正常进行So, in your case:
如果somevariable.value
是null
,则跳过add()
呼叫
如果somevariable.value
不是null
,则对somevariable.value
进行add()
调用