Integer types
- Kotlin course
- Beginner
- Lesson 13/116
- 4 min
Learn Int, Long, Short and Byte types.
Integer types
In Kotlin, integers come in four types: Byte (8 bit), Short (16 bit), Int (32 bit) and Long (64 bit). Int is the most common; type inference picks Int in most cases.
Long is used for very large numbers (e.g., millisecond timestamps). Long literals are written with an L suffix: val big = 100L.
val a: Byte = 12 // -128..127
val b: Short = 1000 // -32768..32767
val c: Int = 100000 // ~±2 billion
val d: Long = 100L // very largePractice Try Long
At the top level, declare val timestamp: Long = 1700000000L. Inside fun main(), print with println(timestamp).
Sample solution
val timestamp: Long = 1700000000L
fun main() {
println(timestamp)
}Expected output
1700000000
Which type to choose?
In most cases Int is enough; its range of about ±2 billion covers most applications. For very large numbers (timestamps, large calculations) you use Long.
Byte and Short are used less; the memory saving is small on modern systems and they are promoted to Int in operations. Still, Byte is common when reading binary data.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is the most common integer type?
- Int Correct answer
- Byte
- Short
- Long
Explanation: Int is the default integer type in most cases.
2 Long suffix
val big = 100____
val big = 100L
Explanation: Long literals are written with the L suffix.
3 Integer types: match the terms
| Term | Meaning |
|---|---|
Byte |
8 bit |
Short |
16 bit |
Int |
32 bit |
Long |
64 bit |