Kotlin has all the standard data types you'd expect, plus some nice touches that make working with them smooth.
Numbers
Kotlin gives you these number types out of the box:
Intโ whole numbers like 42, -10, 0Longโ bigger whole numbers, suffix with L:42LFloatโ decimal numbers with single precision, suffix with f:3.14fDoubleโ decimal numbers with double precision:3.14159Shortโ small whole numbers (-32768 to 32767)Byteโ tiny numbers (-128 to 127)
fun main() {
val age: Int = 25
val population: Long = 8_000_000_000L
val price: Float = 19.99f
val pi: Double = 3.1415926535
println(age)
println(population)
println(price)
println(pi)
}
See the underscores in 8_000_000_000? That's valid Kotlin โ it makes large numbers readable.
Boolean
Good old true and false.
fun main() {
val isKotlinFun: Boolean = true
println("Is Kotlin fun? $isKotlinFun")
}
Char and String
Char is a single character in single quotes. String is text in double quotes.
fun main() {
val letter: Char = 'K'
val message: String = "Kotlin is cool"
println(letter)
println(message.uppercase())
}
Try it Yourself โ