January 2018
Intermediate to advanced
434 pages
14h 1m
English
Let's try an example with the as operator, which is used for casting in Kotlin. It is an unsafe cast operator. The following code example throws ClassCastException, because we cannot convert an integer to string:
fun main(args: Array<String>) { var a : Any = 1 var b = a as String}
On the other hand, the following code runs successfully because of variable a, which, being of Any type, can be cast to String:
fun main(args: Array<String>) { var a : Any = "1" var b = a as String println(b.length)}
Read now
Unlock full access