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
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
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
Long: 25, Double: 25.0, Parsed: 101, Safe: nullKey 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
1. What happens if you write val l: Long = someIntVariable directly in Kotlin?
2. Which function converts an Int to a Double in Kotlin?
3. What does "abc".toInt() do at runtime?
4. What does "abc".toIntOrNull() return?
5. Which function converts a numeric value to its String representation?
Was this page helpful?
You May Also Like
Variables and Data Types in Kotlin
Learn how Kotlin declares variables with val and var, and the built-in data types used to store values.
Operators in Kotlin
Explore Kotlin's arithmetic, comparison, logical, range, and containment operators, including the == vs === distinction.
Null Safety in Kotlin
Kotlin's type system distinguishes nullable from non-nullable types at compile time, eliminating most NullPointerExceptions before code ever runs.
Input and Output in Kotlin
Learn how to print output with println()/print() and read user input with readLine() in Kotlin.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics