Skip to content
Justice Tech School

Type Conversion

Kotlin never converts numbers implicitly. Use toInt(), toDouble() and toIntOrNull().

Example
val a = 7
val d = a.toDouble() / 2
val n = "42".toInt()
val bad = "x".toIntOrNull()
println("$d $n $bad")

Exercise

Safe parse.

"5".()

Quiz Test

What does toIntOrNull give for bad input?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.