Kotlin Basic Data Types
Kotlin's basic numeric types include Byte, Short, Int, Long, Float, and Double. Unlike Java, characters are not considered numeric types and are a separate data type.
Type | Bit Width |
---|---|
Double | 64 |
Float | 32 |
Long | 64 |
Int | 32 |
Short | 16 |
Byte | 8 |
Literal Constants
Here are all types of literal constants:
Decimal: 123
Long integer ending with an uppercase L: 123L
Hexadecimal starting with 0x: 0x0F
Binary starting with 0b: 0b00001011
Note: Octal is not supported
Kotlin also supports traditional symbolic notation for floating-point numbers:
Doubles default notation:
123.5
,123.5e10
Floats use the f or F suffix:
123.5f
You can use underscores to make numeric constants more readable:
val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010
Comparing Two Numbers
In Kotlin, there are no primitive data types, only boxed numeric types. Each variable you define is actually an object encapsulated by Kotlin, which ensures that null pointer exceptions do not occur. This is also true for numeric types, so when comparing two numbers, there is a difference between comparing the size of data and comparing whether two objects are the same.
In Kotlin, three equals signs === indicate comparison of object addresses, and two == indicate comparison of the size of two values.
fun main(args: Array<String>) {
val a: Int = 10000
println(a === a) // true, values are equal, object addresses are equal
// After boxing, two different objects are created
val boxedA: Int? = a
val anotherBoxedA: Int? = a
// Although after boxing, the values are equal, both are 10000
println(boxedA === anotherBoxedA) // false, values are equal, object addresses are different
println(boxedA == anotherBoxedA) // true, values are equal
}
Type Conversion
Due to different representations, smaller types are not subtypes of larger types, and smaller types cannot be implicitly converted to larger types. This means that we cannot assign a Byte value to an Int variable without explicit conversion.
val b: Byte = 1 // OK, literal is statically checked
val i: Int = b // Error
We can use its toInt() method.
val b: Byte = 1 // OK, literal is statically checked
val i: Int = b.toInt() // OK
Each data type has the following methods to convert to other types:
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
In some cases, automatic type conversion is also possible, provided that the correct data type can be inferred from the context and the mathematical operator is overloaded accordingly. For example, the following is correct:
val l = 1L + 3 // Long + Int => Long
Bitwise Operators
For Int and Long types, there is a series of bitwise operators available, namely:
shl(bits) – Left shift (Java’s <<)
shr(bits) – Right shift (Java’s >>)
ushr(bits) – Unsigned right shift (Java’s >>>)
and(bits) – And
or(bits) – Or
xor(bits) – Xor
inv() – Invert
Characters
Unlike Java, Kotlin's Char cannot be directly operated with numbers. Char must be enclosed in single quotes '
.
fun check(c: Char) {
if (c == 1) { // Error: Type mismatch
// …
}
}
Character literals are enclosed in single quotes: '1'. Special characters can be escaped with a backslash. The following escape sequences are supported: \t, \b, \n, \r, \', \", \, and \$. Other characters are encoded using Unicode escape sequence syntax: '\uFF00'.
We can explicitly convert a character to an Int number:
``` fun decimalDigitValue(c: Char): Int { if (c !in '0'..'9') throw IllegalArgumentException(