Kotlin Data Types
Every value in Kotlin has a type, and the type decides what you can do with it. In Kotlin everything is an object — even numbers have functions and properties — but the compiler stores simple values as fast JVM primitives behind the scenes.
Numbers
Whole numbers
| Type | Size | Range |
|---|---|---|
Byte |
8 bits | -128 to 127 |
Short |
16 bits | -32 768 to 32 767 |
Int |
32 bits | about ±2.1 billion |
Long |
64 bits | about ±9.2 quintillion |
Integer literals are Int by default. A value too large for Int becomes Long automatically, and you can force Long with an L suffix.
Decimal numbers
| Type | Size | Precision |
|---|---|---|
Float |
32 bits | ~6–7 decimal digits, literal needs f or F |
Double |
64 bits | ~15–16 decimal digits (the default) |
fun main() {
val apples = 12 // Int
val stars = 8_000_000_000 // too big for Int -> Long
val fileSize = 512L // Long
val price = 4.99 // Double
val ratio = 0.75f // Float
println(apples::class.simpleName)
println(stars::class.simpleName)
println(fileSize::class.simpleName)
println(price::class.simpleName)
println(ratio::class.simpleName)
}Int
Long
Long
Double
Float
Tip: Underscores in numeric literals (
1_000_000) are ignored by the compiler and make big numbers readable.
Each number type knows its limits:
fun main() {
println("Int: ${Int.MIN_VALUE} .. ${Int.MAX_VALUE}")
println("Byte: ${Byte.MIN_VALUE} .. ${Byte.MAX_VALUE}")
println("Long max: ${Long.MAX_VALUE}")
}Int: -2147483648 .. 2147483647
Byte: -128 .. 127
Long max: 9223372036854775807
Kotlin also has unsigned types (UByte, UShort, UInt, ULong) written with a u suffix, e.g. 255u. They are mainly used for low-level work like bytes from files or colours.
Booleans
Boolean holds true or false. It's the result of comparisons and the type of every condition.
Characters
A Char is a single character in single quotes: 'A', '7', '€'. Note that 'A' (a Char) and "A" (a String) are different types.
Strings
A String is text in double quotes. Strings are covered in depth in their own chapter.
fun main() {
val isOpen: Boolean = true
val grade: Char = 'B'
val shop: String = "Corner Bakery"
println("$shop is open: $isOpen")
println("Hygiene grade: $grade")
println("Next letter: ${grade + 1}")
}Corner Bakery is open: true
Hygiene grade: B
Next letter: C
Arrays
An Array holds a fixed number of values of the same type, created for example with arrayOf("a", "b"). They get their own chapter later.
Any, Unit and Nothing
| Type | Meaning |
|---|---|
Any |
the root of all non-nullable types (like Java's Object) |
Unit |
"no meaningful value" — the return type of functions that return nothing (like void) |
Nothing |
a type with no values, used for functions that never return (they always throw) |
Overflow
Whole-number types wrap around silently when a result is too large. If you expect big numbers, use Long:
fun main() {
val big = Int.MAX_VALUE
println(big + 1)
println(big.toLong() + 1)
}-2147483648
2147483648
Note: Kotlin never converts numbers implicitly — you cannot assign an
Intto aLongvariable without calling.toLong(). The next chapter explains type conversion.
Test yourself
What is the default type of the literal 42?
- Byte
- Int
- Long
- Number
How do you write a Float literal?
- 3.14
- 3.14f
- 3.14d
- float(3.14)
What is the difference between 'K' and "K"?
- None
- 'K' is a Char, "K" is a String
- 'K' is a String, "K" is a Char
- 'K' is invalid