In Kotlin, almost everything is an object, including numbers, characters, and boolean types. Even though numbers may be represented using primitive types at the lower level, they can still be used like ordinary classes.
Tips: Hold down Ctrl and click the left mouse button to view Kotlin's source code in IntelliJ IDEA.
Numbers include integer types
and floating-point types
.
Kotlin provides a set of built-in types to represent numbers. For integers, there are four different-sized types, each with its own range of values.
Type | Size (bits) | Minimum Value | Maximum Value |
---|---|---|---|
Byte | 8 | -128 | 127 |
Short | 16 | -32768 | 32767 |
Int | 32 | -2,147,483,648 | 2,147,483,647 |
Long | 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
When you define a variable without specifying its type, Kotlin's compiler automatically infers the type.
xxxxxxxxxx
val one = 1 // Int
val threeBillion = 3000000000 // Long
val oneLong = 1L // Long
val oneByte: Byte = 1