Skip to content

Kotlin General Notes

Compose Desktop/Mobile UI Skia

Types =====

Numbers

  • Byte: 8, -128..128
  • Short: 32768 .. 32767
  • Int: -2,147,483,648 (-231) .. 2,147,483,647 (231 - 1)
  • Long: -9,223,372,036,854,775,808 (-263) .. 9,223,372,036,854,775,807 (263 - 1)
  • Float: size 32
  • Double: size 64
//
val one = 1 // Int
val threeBillion = 3000000000 // Long
val oneLong = 1L // Long
val oneByte: Byte = 1
//
val pi = 3.14 // Double
val e = 2.7182818284 // Double
val eFloat = 2.7182818284f // Float, actual value is 2.7182817
//
fun main() {
fun printDouble(d: Double) { print(d) }
val i = 1
val d = 1.1
val f = 1.1f
printDouble(d)
// printDouble(i) // Error: Type mismatch
// printDouble(f) // Error: Type mismatch
}
// Underscores in numeric literals
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
// Hypothetical code, does not actually compile:
val a: Int? = 1 // A boxed Int (java.lang.Integer)
val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long)
print(b == a) // Surprise! This prints "false" as Long's equals() checks whether the other is Long as well

Literal constants

  • Decimals: 123, Longs tagged with L
  • Hexadecimals: Ox0F
  • Binaries: 0b000001011
  • Octal: Not supported
  • Doubles by default: 123.5, 123.5e10

Explicit conversions

  • toByte():Byte
  • toInt():Int
  • toLong():Long
  • toFloat:Float
  • toDouble:Double
  • toChar():Char
val l = 1L + 3 // Long + Int => Long

Characters

  • Char cannot be treated as a number.
  • Character literals go in single quotes: ‘1’
  • Supported escape sequences: \t, \b, \n, \r, \', \", \\ and \$
fun check(c: Char) {
if (c == 1) { // ERROR: incompatible types
// ...
}
}

Booleans

  • two values: true and false
  • || – lazy disjunction
  • && – lazy conjunction
  • ! - negation

Array

  • Array has get/set access that is implemented by []
  • Array has size for length.