Back to questions
JuniorTheoryCommon
What happens when you build and run this program?
fun main() {
val small: Int = 5
val big: Long = small
println(big)
}It does not compile: an Int does not turn into a Long by itself
- Expecting
5: aLongvariable accepts only aLong, and Kotlin never converts anIntfor you; writeval big: Long = small.toLong(). - Expecting a run-time failure: types are checked while building, so the compiler reports
initializer type mismatchand nothing runs.
- How do you fix the second line so that the program prints
5? - Why does
val big: Long = 5compile whileval big: Long = smalldoes not?