Kotlin Tutorial · Kotlin Basics · Lesson 5 of 31

Kotlin Data Types

Numbers, booleans, characters and strings, type conversion, and how Kotlin maps them to the JVM.

Create a free account to save this page, take notes and track your progress. Sign in

Basic types

Type Example Notes
Int, Long 42, 3_000_000_000L 32-bit and 64-bit integers
Double, Float 3.14, 2.5f floating point
Boolean true
Char 'K' a single character
String "Kotlin" immutable text

Everything is an object (you can call 42.toString()), but the compiler uses fast JVM primitives where it can.

Basic types
fun main() {
    val users = 42
    val population = 3_000_000_000L
    val ratio = 0.75
    val active = true
    val grade = 'A'
    println("$users ${users::class.simpleName}")
    println("$population ${population::class.simpleName}")
    println("$ratio $active $grade")
    println(Int.MAX_VALUE)
}

Output

42 Int
3000000000 Long
0.75 true A
2147483647

Run this on your computer — see “Get Started” for setup.

No implicit conversions

Kotlin never converts numbers silently: an Int isn't assigned to a Long without toLong(). Integer division truncates; use a Double to get a fraction.

Conversions and arithmetic
fun main() {
    val items = 7
    val boxes = 2
    println(items / boxes)               // integer division
    println(items % boxes)               // remainder
    println(items.toDouble() / boxes)    // convert first
    val total: Long = items.toLong() * 1_000_000_000
    println(total)
    println("42".toInt() + 1)
    println("abc".toIntOrNull())
}

Output

3
1
3.5
7000000000
43
null

Run this on your computer — see “Get Started” for setup.

Test yourself

What does 7 / 2 evaluate to when both are Int?

  1. 3.5
  2. 3
  3. 4
  4. A compile error
Show answer

Answer: 3

Was this page helpful?