zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[Kotlin] Typecheck with 'is' keyword, 'as' keyword for assert type

Kotlin for &# is with 39 type as
2023-09-14 08:59:13 时间
val randomNumber = Random().nextInt(3)

if (randomNumber is BigDecimal) {
    result = result.add(BigDecimal(36))
}

If you use type check, then 'result' is auto casting to BigDecimal type.

 

'as' keyword

val randomNumber = Random().nextInt(3)

if (randomNumber is BigDecimal) {
    result = result.add(BigDecimal(36))
} else {
    val tempResult: String = result as String
    result = tempResult.toUpperCase()
}