variables - TonyGuyot/kotlin-notes GitHub Wiki

Variables and types

Defining variables

  • val a: Int = 1 <- define type and value
  • val a = 1 <- shorter version, type is inferred from value
  • var a: Int <- type only, value must be set later

There are two keywords to define a variable:

  • val for read-only variables (re-assignment of value is prohibited)
  • var for a mutable variable

Basic types

keyword description
Byte 8-bit integer
Short 16-bit integer
Int 32-bit integer
Long 64-bit integer
Float 32-bit floating point number
Double 64-bit floating point number
Char single character
Boolean 16-bit integer
String character string (immutable)