是“双重感叹"吗?与"as"相同在科特林? [英] Is "double exclamation" same as "as" in Kotlin?
本文介绍了是“双重感叹"吗?与"as"相同在科特林?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有像var text: String?
这样的变量.
Assume that we have variable like var text: String?
.
当我要将其强制转换为不可为null的类型时,可以使用:
When I want to cast it to non nullable type I can use:
-
text!!
-
text as String
text!!
text as String
它们的意思相同吗?
我知道,如果text
是null
,这些方法将引发异常.
I know that these ways will throw an exception if text
is null
.
推荐答案
它们几乎相同,但是第一个将为null
抛出KotlinNullPointerExcption
,第二个将为TypeCastException
抛出.
They are almost the same, but first one will throw KotlinNullPointerExcption
for null
, and second one will throw TypeCastException
.
这篇关于是“双重感叹"吗?与"as"相同在科特林?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文