What is difference between “as” and “is” operator in Kotlin?
问题 In Java, I can write code like: void cast(A a) { if(a instanceof Person) { Person p = (Person) a; } } In Kotlin, what should I do? Use as operator or is operator? 回答1: is X is the equivalent of instanceof X foo as X is the equivalent of ((X) foo) Additionally, Kotlin performs smart casting where possible, so no additional cast needed after you check the type using is : open class Person : A() { val foo: Int = 42 } open class A and then: if (p is Person) { println(p.foo) // look, no cast