Basic syntax in Kotlin

Package definition and imports

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

package my.demo

import kotlin.text.*

// ...
Copied!

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

See Packages.

Program entry point

An entry point of a Kotlin application is the main function.

Another form of main accepts a variable number of String arguments.

print prints its argument to the standard output.

println prints its arguments and adds a line break, so that the next thing you print appears on the next line.

Functions

A function with two Int parameters and Int return type.


A function body can be an expression. Its return type is inferred.


A function that returns no meaningful value.


Unit return type can be omitted.


See Functions.

Variables

Read-only local variables are defined using the keyword val. They can be assigned a value only once.

Variables that can be reassigned use the var keyword.


You can declare variables at the top level.


See also Properties.

Creating classes and instances

To define a class, use the class keyword.

class Shape
Copied!

Properties of a class can be listed in its declaration or body.

class Rectangle(var height: Double, var length: Double) {
    var perimeter = (height + length) * 2
}
Copied!

The default constructor with parameters listed in the class declaration is available automatically.


Inheritance between classes is declared by a colon (: ). Classes are final by default; to make a class inheritable, mark it as open.

open class Shape

class Rectangle(var height: Double, var length: Double): Shape {
    var perimeter = (height + length) * 2
}
Copied!

See classes and objects and instances.

Comments

Just like most modern languages, Kotlin supports single-line (or end-of-line) and multi-line (block) comments.

// This is an end-of-line comment

/* This is a block comment
   on multiple lines. */
Copied!

Block comments in Kotlin can be nested.

/* The comment starts here
/* contains a nested comment */
and ends here. */
Copied!

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

String templates


See String templates for details.

Conditional expressions


In Kotlin, if can also be used as an expression.


See if -expressions.

for loop

Target platform: JVMRunning on kotlin v.1.4.30

or

Target platform: JVMRunning on kotlin v.1.4.30

See for loop.

while loop

Target platform: JVMRunning on kotlin v.1.4.30

See while loop.

when expression


Target platform: JVMRunning on kotlin v.1.4.30

See when expression.

Ranges

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

Check if a number is out of range.

Iterate over a range.

Or over a progression.

Collections

Iterate over a collection.

Check if a collection contains an object using in operator.


Using lambda expressions to filter and map collections:


Nullable values and null checks

A reference must be explicitly marked as nullable when null value is possible. Nullable type names have ? at the end.

Return null if str does not hold an integer:

fun parseInt(str: String): Int? {
    // ...
}
Copied!

Use a function returning nullable value:

or

Target platform: JVMRunning on kotlin v.1.4.30

See Null-safety.

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:

or

or even

See Classes and Type casts.


0 Comments

Brand creation, trend analysis & style consulting

Lorem Ipsum has been the industry's standard dummy text ever since. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since.