Kotlin Data Types
Every value in Kotlin has a type. The type tells the computer what kind of data is stored and how much memory to reserve for it. Kotlin's type system is strict — you cannot accidentally mix incompatible types.
The Main Data Types
┌─────────────────────────────────────────────────────────┐
│ Kotlin Basic Data Types │
├────────────┬──────────────┬──────────────────────────── │
│ Category │ Type │ Example Value │
├────────────┼──────────────┼──────────────────────────── │
│ Whole │ Byte │ 100 (range: -128 to 127) │
│ Numbers │ Short │ 30000 │
│ │ Int │ 2_000_000 │
│ │ Long │ 8_000_000_000L │
├────────────┼──────────────┼──────────────────────────── │
│ Decimal │ Float │ 3.14f │
│ Numbers │ Double │ 3.14159265 │
├────────────┼──────────────┼──────────────────────────── │
│ Text │ Char │ 'A' │
│ │ String │ "Hello" │
├────────────┼──────────────┼──────────────────────────── │
│ Logic │ Boolean │ true / false │
└────────────┴──────────────┴──────────────────────────── ┘
Integer Types
Use Int for most whole number needs. Switch to Long only when your number exceeds 2 billion.
val age: Byte = 25 // small numbers (-128 to 127)
val year: Short = 2024 // medium numbers
val population: Int = 1_400_000 // most common choice
val nationalDebt: Long = 33_000_000_000_000L // very large numbersThe L at the end of a number tells Kotlin it is a Long value. Underscores inside numbers are allowed for readability — 1_000_000 is the same as 1000000.
Decimal Types
Use Double for most decimal calculations. It offers 15–16 digits of precision. Use Float only when memory is very limited — it has only 6–7 digits of precision.
val price: Double = 99.99 // default choice for decimals
val discount: Float = 0.15f // f suffix marks it as FloatMemory Size Comparison
Type │ Memory │ Range
──────────┼─────────┼──────────────────────────────
Byte │ 1 byte │ -128 to 127
Short │ 2 bytes │ -32,768 to 32,767
Int │ 4 bytes │ -2.1 billion to 2.1 billion
Long │ 8 bytes │ ±9.2 quintillion
Float │ 4 bytes │ 6-7 decimal digits of precision
Double │ 8 bytes │ 15-16 decimal digits of precision
Char │ 2 bytes │ one Unicode character
Boolean │ 1 bit │ true or false
Char Type
A Char holds exactly one character. Char values use single quotes, not double quotes.
val grade: Char = 'A'
val symbol: Char = '@'
val digit: Char = '7'
// This is wrong:
// val letter: Char = "A" ← double quotes make it a StringBoolean Type
A Boolean stores only two possible values: true or false. These are the building blocks of all decision-making in code.
val isLoggedIn: Boolean = true
val hasDiscount: Boolean = false
val isAdult: Boolean = age >= 18 // evaluates to true or falseString Type
A String is a sequence of characters. Strings use double quotes. They can contain letters, numbers, spaces, and symbols.
val firstName: String = "Maria"
val sentence: String = "Kotlin is fun to learn."
val mixed: String = "Order #1024 - Total: $99.99"Type Conversion
Kotlin does not automatically convert one type to another. You must convert explicitly using built-in functions:
val number: Int = 42
val asDouble: Double = number.toDouble() // 42.0
val asString: String = number.toString() // "42"
val asLong: Long = number.toLong() // 42L
val text = "123"
val parsed: Int = text.toInt() // 123Checking the Type of a Variable
Use the is keyword to check whether a value belongs to a specific type:
val value: Any = 42
if (value is Int) {
println("It is an Int: $value")
}
if (value !is String) {
println("It is not a String")
}Practical Example: Shopping Cart
fun main() {
val itemName: String = "Headphones"
val quantity: Int = 2
val unitPrice: Double = 1499.99
val isMember: Boolean = true
val memberDiscount: Float = 0.10f
val subtotal = quantity * unitPrice
val discount = if (isMember) subtotal * memberDiscount else 0.0
val total = subtotal - discount
println("Item: $itemName")
println("Quantity: $quantity")
println("Subtotal: $subtotal")
println("Discount: $discount")
println("Total: $total")
}Output:
Item: Headphones
Quantity: 2
Subtotal: 2999.98
Discount: 299.998
Total: 2699.982