Basic Syntax

Defining packages

Package specification should be at the top of the source file:

package my.demo

import java.util.*

// ...

It is not required to match directories and packages: source files can be placed arbitrarily in the file system.

See Packages.

Defining functions

Function having two Int parameters with Int return type:

fun sum(a: Int, b: Int): Int {
    return a + b
}

Target platform: JVMRunning on kotlin v. 1.1.2

Function with an expression body and inferred return type:

fun sum(a: Int, b: Int) = a + b

Target platform: JVMRunning on kotlin v. 1.1.2

Function returning no meaningful value:

Target platform: JVMRunning on kotlin v. 1.1.2

Unit return type can be omitted:

Target platform: JVMRunning on kotlin v. 1.1.2

See Functions.

Defining local variables

Assign-once (read-only) local variable:

Target platform: JVMRunning on kotlin v. 1.1.2

Mutable variable:

Target platform: JVMRunning on kotlin v. 1.1.2

See also Properties And Fields.

Comments

Just like Java and JavaScript, Kotlin supports end-of-line and block comments.

Unlike Java, block comments in Kotlin can be nested.

See Documenting Kotlin Code for information on the documentation comment syntax.

Using string templates

Target platform: JVMRunning on kotlin v. 1.1.2

See String templates.

Using conditional expressions

Target platform: JVMRunning on kotlin v. 1.1.2

Using if as an expression:

Target platform: JVMRunning on kotlin v. 1.1.2

See if-expressions.

Using nullable values and checking for null

A reference must be explicitly marked as nullable when null value is possible.

Return null if str does not hold an integer:

Use a function returning nullable value:

Target platform: JVMRunning on kotlin v. 1.1.2

or

Target platform: JVMRunning on kotlin v. 1.1.2

See Null-safety.

Using type checks and automatic casts

The is operator checks if an expression is an instance of a type. If an immutable local variable or property is checked for a specific type, there's no need to cast it explicitly:

Target platform: JVMRunning on kotlin v. 1.1.2 or

Target platform: JVMRunning on kotlin v. 1.1.2

or even

Target platform: JVMRunning on kotlin v. 1.1.2

See Classes and Type casts.

Using a for loop

Target platform: JVMRunning on kotlin v. 1.1.2

or

Target platform: JVMRunning on kotlin v. 1.1.2

See for loop.

Using a while loop

Target platform: JVMRunning on kotlin v. 1.1.2

See while loop.

Using when expression

Target platform: JVMRunning on kotlin v. 1.1.2

See when expression.

Using ranges

Check if a number is within a range using in operator:

val x = 10 val y = 9 if (x in 1..y+1) { println("fits in range") }

Target platform: JVMRunning on kotlin v. 1.1.2

or over a progression:

Target platform: JVMRunning on kotlin v. 1.1.2

See Ranges.

Using collections

Iterating over a collection:

Target platform: JVMRunning on kotlin v. 1.1.2

Checking if a collection contains an object using in operator:

Target platform: JVMRunning on kotlin v. 1.1.2

Using lambda expressions to filter and map collections:

Target platform: JVMRunning on kotlin v. 1.1.2

See Higher-order functions and Lambdas.

Last updated

Was this helpful?