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

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()
}
原文地址:https://www.cnblogs.com/Answer1215/p/13880068.html