Every value in Kotlin has a type. The type decides what kind of data a variable can hold (a number, a letter, true/false…) and what you can do with it. In this lesson you’ll learn the basic types you will use in every Kotlin program.
Page Contents
Type inference: Kotlin can guess the type
You usually don’t have to write the type. Kotlin works it out from the value:
fun main() {
val age = 25 // Int
val price = 99.5 // Double
val grade = 'A' // Char
val isPassed = true // Boolean
val name = "Asha" // String
println(age::class.simpleName)
println(price::class.simpleName)
println(grade::class.simpleName)
println(isPassed::class.simpleName)
println(name::class.simpleName)
}Output:
Int Double Char Boolean String
You can also write the type yourself after a colon: val age: Int = 25. You must do this when you declare a variable without a value.
Whole numbers: Byte, Short, Int, Long
| Type | Size | Range |
|---|---|---|
Byte | 8 bits | -128 to 127 |
Short | 16 bits | -32,768 to 32,767 |
Int | 32 bits | about -2.1 billion to 2.1 billion |
Long | 64 bits | about ±9.2 quintillion |
Int is the default. Use Long for very large numbers, such as timestamps in milliseconds, by adding L to the end:
fun main() {
val small = 100 // Int
val big = 3_000_000_000L // Long (too big for Int)
val millis = System.currentTimeMillis() // Long
println(Int.MAX_VALUE)
println(Long.MAX_VALUE)
println(big)
println(millis > 0)
}Output:
2147483647 9223372036854775807 3000000000 true
Underscores like 3_000_000_000 make big numbers easier to read. They don’t change the value.
Decimal numbers: Double and Float
| Type | Size | Precision |
|---|---|---|
Double | 64 bits | about 15–16 digits |
Float | 32 bits | about 6–7 digits |
A number with a decimal point is a Double by default. Add f to make it a Float:
fun main() {
val pi = 3.14159265358979 // Double
val ratio = 3.14159265358979f // Float
println(pi)
println(ratio)
println(10 / 4) // Int division
println(10 / 4.0) // Double division
}Output:
3.14159265358979 3.1415927 2 2.5
Notice that 10 / 4 is 2, not 2.5. When both numbers are Int, the result is an Int and the decimal part is dropped. Make one of them a Double to get a decimal answer.
Char: a single character
A Char holds one character in single quotes:
fun main() {
val letter = 'K'
val digit = '7'
println(letter.isLetter())
println(digit.isDigit())
println(letter.lowercaseChar())
println(digit.digitToInt() + 1)
}Output:
true true k 8
Boolean: true or false
fun main() {
val isLoggedIn = true
val hasPremium = false
println(isLoggedIn && hasPremium) // and
println(isLoggedIn || hasPremium) // or
println(!hasPremium) // not
}Output:
false true true
You’ll use Booleans all the time with if and when in the control-flow lessons.
String: text
A String is text in double quotes. Strings get their own lesson later in this module, but here’s a taste:
fun main() {
val language = "Kotlin"
println(language.length)
println(language.uppercase())
println("I am learning $language")
}Output:
6 KOTLIN I am learning Kotlin
Everything is an object
In Kotlin, even numbers have functions you can call, such as 25.toString() or 'a'.uppercaseChar(). There are no separate “primitive” and “wrapper” types like Java’s int and Integer. The compiler still uses fast primitives behind the scenes where it can.
Common mistakes
- Using double quotes for a Char:
val c: Char = "A"doesn’t compile. Use'A'. - Integer overflow:
Int.MAX_VALUE + 1wraps around to a negative number without any error. UseLongfor big values. - Expecting decimals from Int division:
7 / 2is3. Write7 / 2.0or7.toDouble() / 2. - Mixing types without converting: an
Intcan’t be assigned to aLongvariable directly. You’ll learn to convert in the next lesson.
Exercise
Create variables for a product: its name, its price (with decimals), the quantity in stock and whether it’s on sale. Print the total stock value (price × quantity).
fun main() {
val productName = "Headphones"
val price = 1499.0
val quantity = 12
val onSale = true
println("$productName on sale: $onSale")
println("Stock value: ${price * quantity}")
}Output:
Headphones on sale: true Stock value: 17988.0
FAQ
What is the default number type in Kotlin?
Int for whole numbers and Double for decimal numbers.
Does Kotlin have primitive types?
Not in the code you write: every type is an object with functions. The compiler turns Int, Double and the others into JVM primitives where it can, for speed.
When should I use Long instead of Int?
When a value can go above about 2.1 billion, for example timestamps in milliseconds, file sizes in bytes or large IDs.
