Part 02 / Topic 2 of 8
Basic types and type inference
Kotlin has the same core numeric and text types Java does, under cleaner names, and it almost
always works out the type for you from the value on the right of = — this is type
inference, not "no types."
| Type | Holds | Example literal |
|---|---|---|
Int |
a 32-bit whole number | 7 |
Long |
a 64-bit whole number, for values too big for Int |
2_450_000_000L |
Double |
a 64-bit decimal number | 5.0 |
Boolean |
true or false |
true |
Char |
one character | 'A' |
String |
text | "Arjun" |
fun main() {
val tableNumber = 7 // inferred as Int
val coversToday = 2_450_000_000L // inferred as Long: too big for Int, needs the L suffix
val gstPercent = 5.0 // inferred as Double
val isOpenNow = true // inferred as Boolean
val billLetter = 'A' // inferred as Char
val waiterName = "Arjun" // inferred as String
println("Table: $tableNumber (${tableNumber::class.simpleName})")
println("Covers served this year: $coversToday (${coversToday::class.simpleName})")
println("GST percent: $gstPercent (${gstPercent::class.simpleName})")
println("Open now: $isOpenNow (${isOpenNow::class.simpleName})")
println("Bill series: $billLetter (${billLetter::class.simpleName})")
println("Waiter: $waiterName (${waiterName::class.simpleName})")
println("Int.MAX_VALUE = ${Int.MAX_VALUE}")
// Numbers never convert implicitly in Kotlin, even when it looks safe
val coversAsInt = 1200
val coversAsLong: Long = coversAsInt.toLong() // widening still needs an explicit call
println("coversAsInt as Long: $coversAsLong")
// Narrowing truncates, it does not round
val averageBill = 453.9
val roundedDownRupees = averageBill.toInt()
println("Average bill: $averageBill, truncated to Int: $roundedDownRupees")
}
$ kotlinc 02_basic_types.kt -include-runtime -d 02.jar
$ java -jar 02.jar
Table: 7 (Int)
Covers served this year: 2450000000 (Long)
GST percent: 5.0 (Double)
Open now: true (Boolean)
Bill series: A (Char)
Waiter: Arjun (String)
Int.MAX_VALUE = 2147483647
coversAsInt as Long: 1200
Average bill: 453.9, truncated to Int: 453
Two details worth slowing down on:
::class.simpleNameproves inference, it doesn't guess it. Each value above never got an explicit type annotation, yet the class name printed is exactly the type from the table —kotlincworked it out from the literal, once, at compile time.- Widening still needs
.toLong(). Unlike Java, which quietly widens anIntto aLong, Kotlin never converts numeric types for you, not even in the "obviously safe" direction. Try it without the call:
fun main() {
val coversAsInt: Int = 1200
val coversAsLong: Long = coversAsInt
println(coversAsLong)
}
$ kotlinc TypeError.kt -d out
TypeError.kt:3:28: error: initializer type mismatch: expected 'Long', actual 'Int'.
val coversAsLong: Long = coversAsInt
^
averageBill.toInt() shows the other direction: narrowing a Double to an Int truncates
the decimal part (453.9 becomes 453), it does not round to 454. Rounding is a deliberate
choice (kotlin.math.round, used later in this course), not something toInt() does for you.
🔍 Going deeper: Kotlin also has
Short,ByteandFloat, smaller cousins ofIntandDouble. They exist for memory-constrained or interop cases;Int/Long/Doublecover everyday Kotlin, so this course only reaches for the smaller types when a specific reason comes up.
Full program: examples/02_basic_types.kt.
Done here? The practice set for Part 2 — including the solutions — is on the part page.