Careeroza
Overview of Basic Types
Everything is an Object (on the Surface)
In Kotlin there are no separate primitive types like Java's int. You always write Int, Double, Boolean, and you can call functions on them (5.toString()). Behind the scenes the compiler still uses fast JVM primitives where it can.
| Type | Size | Example | Java equivalent |
|---|---|---|---|
Byte | 8 bit | val b: Byte = 100 | byte |
Short | 16 bit | val s: Short = 2000 | short |
Int | 32 bit | val i = 42 | int |
Long | 64 bit | val l = 42L | long |
Float | 32 bit | val f = 4.5f | float |
Double | 64 bit | val d = 4.5 | double |
Char | 16 bit | val c = 'K' | char |
Boolean | - | val ok = true | boolean |
String | - | val s = "hi" | String |
Calling Functions on Values
Example
Even a literal number has functions.
fun main() {
println(5.toString() + "!")
println(3.coerceAtLeast(10))
println(7.toDouble())
println('a'.uppercaseChar())
println(true.not())
println(255.toString(2)) // 255 written in binary
}Output
5! 10 7.0 A false 11111111