Kotlin

To share this page click on the buttons below;

Variables and constant

In Kotlin variables are identified by the reserved word var: the content of variables can change. Immutable variable (or value or simply constant) are identified by the reserved word val: the content of values cannot change.

The strict separation between constants (immutable variable) and variables is for performances reasons.

Declaration of variables

To declare a variable of a certain type use:

val <variable_name>: <variable_type> = <variable_value>

: <variable_type> can be omitted (the compiler infers the type from the value).

Even if inferred the initial type of a variable is fixed and cannot be changed.

Declaration of constants

To declare a constant of a certain type use:

var <value_name>: <value_type> = <value>

: <variable_type> can be omitted (the compiler infers the type from the value)

Types

Numeric types

  • Double 64 bits wide
  • Float 32 bits wide
  • Long 64 bits wide
  • Int 32 bits wide
  • Short 16 bits wide
  • Byte 8 bits wide

Character

Char

Boolean

Boolean -> values true and false

String

String

Type conversion

To convert type to another every type has the following function:

  • toChar() to convert a type to Char type
  • toInt() to convert a type to Int type
  • toLong() to convert a type to Long type
  • toFloat() to convert a type to Float type
  • toDouble() to convert a type to Double type
  • toByte() to convert a type to Byte type
  • toShort() to convert a type to Short type

Nullable types

Kotlin defines nullable types, those types have the same name of standards types but they are followed by ? (Int -> Int?, String -> String?, and so on). Only nullable types variable can be assigned to null.

Safe check

If a variable is nullable then it has to be checked if it null before to use it. If a variable is declared as var text:String? it is forbidden to ask for the length using text.length without checking if text is actually null. For that purpose Kotlin uses the ? operator which is applied to the nullable variable. The safe access operator ? accesses the nullable variables only if they are not null.

Elvis operator

The elvis operator ?: return what is on the left if is not null otherwise it return what is on the right.

Non null assertion

The operator !! convert a nullable types into a non nullable one. If applied to null it throws an exception.

Ranges

Ranges are quick way to create sequences of numbers:

<range> = <start value> .. <stop value> 

that makes a sequence of values from <start value> to <stop value> both included.

Ranges can be defined also with:

<range> = <start value>.rangeTo(<stop value>) // for increasing sequences
<range> = <start value>.downTo(<stop value>) // for decreasing sequences

To define a step for increasing the sequence:

<range> = <start value> .. <stop value> 
<range1> = <range>.step(<step value>)

To reverse a range

<range> = <start value> .. <stop value> 
<range1> = <range>.reversed()

Any

  • Any type in Kotlin represents the father-type of any non-nullable type.
  • basic non-nullable types are derived from Any.
  • Any can't hold null.

Unit

  • Unit type is equivalent to the C type void.
  • A function that does not return anything returns Unit.
  • Unit is a type.
  • It is returned implicitly, so that there is no need for return.

Nothing

Nothing is a type used to specify that a function will never terminate normally (for example because it always throws an exception).

To share this page click on the buttons below;