Types

base types

type description
Byte Represents 8-bit integer number.
Short Represents 16-bit integer number.
Int Represents 32-bit integer number.
Long Represents 64-bit integer number.
Float Represents 32-bit single-precision floating-point number.
Double Represents 64-bit double-precision floating-point number.
Char Represents single character.
String Represents immutable string.
StringBuilder Represents "mutable" string.
Boolean Represents boolean type with two values: true and false.
List Generic interface that represents immutable list of items. To create instance you can use any Java list classes or listOf()/List() functions (ArrayList will be used internally). Don't confuse with Java List interface.
MutableList Generic interface that represents mutable list of items. This is extension of List. To create instance you can use any Java list classes or mutableListOf()/MutableList() functions (ArrayList will be used internally).
Set Generic interface that represents immutable set of items. Don't confuse with Java Set interface. To create instance you can use any Java set classes or setOf()/hashSetOf()/linkedSetOf() functions. Internally EmptySet, HashSet and LinkedHashSet classes will be used.
MutableSet Generic interface that represents mutable set of items. This is extensions of Set. To create instance you can use any Java set classes or mutableSetOf()/hashSetOf()/linkedSetOf() functions.
Map Generic interface that represents immutable map. Don't confuse with Java Map interface. To create instance you can use any Java map classes or mapOf()/buildMap() functions. Internally EmptyMap and LinkedHashMap will be used.
MutableMap Generic interface that represents mutable map. This is extensions of Map. To create instance you can use any Java map classes or mutableMapOf()/linkedMapOf()/hashMapOf() functions.

special types

The Unit object is the default return type for functions. But it differs from void or Void class in Java,

  • it is a type and therefore can be used as a type argument
  • has only one value
  • it is returned implicitly, no need of a return statement

The Any class is the root of the Kotlin class hierarchy. Every Kotlin class has Any as a superclass. And all types are objects, although some of the types like numbers can be represented as primitive values at runtime.

java.lang.Object inherits toString(), equals() and hasCode() methods from Any. If you want to use wait() and notify() like in Java, you must cast your variable to Object.

The Nothing class has no instances. You can use Nothing to represent "a value that never exists". It is used when a function will never terminate normally and therefore a return value has no sense.

fun fail(message: String): Nothing {
    throw IllegalStateException(message)
}

Java classes

You can use any Java class in Kotlin code.

Via extension feature Kotlin adds additional methods to standard Java classes. For example, File.writeText().