100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Type Conversion in Kotlin

Understand why Kotlin requires explicit conversion between numeric types and how to use functions like toInt() and toDouble().

BasicsBeginner7 min readJul 8, 2026
Analogies

Introduction

Unlike many languages, Kotlin does not perform implicit (automatic) conversions between numeric types. Even though an Int can conceptually fit inside a Long, Kotlin will not silently widen it for you. Instead, Kotlin requires explicit conversion using functions like toInt(), toLong(), toDouble(), toFloat(), toShort(), toByte(), and toString(). This design choice avoids subtle bugs caused by unnoticed precision loss or type-widening surprises.

🏏

Cricket analogy: Kotlin won't silently treat a bowler's over count (Int) as a match total (Long) the way a scorer might round figures casually; you must explicitly call toLong(), just as a scoreboard operator must deliberately re-enter a stat in a new format.

Syntax

kotlin
val i: Int = 10
val l: Long = i.toLong()
val d: Double = i.toDouble()
val s: String = i.toString()
val backToInt: Int = "42".toInt()

Explanation

If you write val l: Long = i where i is an Int, Kotlin raises a compile-time type mismatch error — there is no automatic widening from Int to Long, even though Long can hold every possible Int value. You must call i.toLong() explicitly. The same applies in the other direction, and between all numeric types. Converting a String to a number uses functions like toInt() or toDouble(), which throw a NumberFormatException if the string is not a valid number; toIntOrNull() is a safer alternative that returns null instead of throwing. Converting any value to text uses toString().

🏏

Cricket analogy: Assigning an Int over count to a Long total triggers a compile error until you call overs.toLong(); parsing a scoreboard string like '250' with toIntOrNull() safely returns null instead of crashing if the board reads 'RAIN'.

Example

kotlin
fun main() {
    val age: Int = 25
    val ageAsLong: Long = age.toLong()
    val ageAsDouble: Double = age.toDouble()

    val input = "101"
    val parsed: Int = input.toInt()

    val invalid = "abc"
    val safeParsed: Int? = invalid.toIntOrNull()

    println("Long: $ageAsLong, Double: $ageAsDouble, Parsed: $parsed, Safe: $safeParsed")
}

Output

text
Long: 25, Double: 25.0, Parsed: 101, Safe: null

Key Takeaways

  • Kotlin never performs implicit conversions between numeric types, even for widening (e.g. Int to Long).
  • Use explicit conversion functions: toInt(), toLong(), toDouble(), toFloat(), toShort(), toByte(), toString().
  • Converting a String to a number with toInt()/toDouble() throws NumberFormatException on invalid input.
  • toIntOrNull() and similar *OrNull() functions return null instead of throwing for invalid input.
  • Explicit conversion prevents hidden precision loss and makes type changes visible in the code.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#TypeConversionInKotlin#Type#Conversion#Syntax#Explanation#StudyNotes#SkillVeris