Skip to content

Kotlin

Package & Imports

Kotlin
1
2
3
package com.app.uniqueID

import <package>

Variable & Constants

Kotlin
1
2
3
4
var variable: Type    //variable declaration
var variable = value    //type can be omitted if it can be deduced by initialization

val CONSTANT_NAME: Type = value    //constant declaration

Nullable Variables

For a variable to hold a null value, it must be of a nullable type.
Nullable types are specified suffixing ? to the variable type.

Kotlin
1
2
3
4
5
6
7
var nullableVariable: Type? = null

nullableVariable?.method()    //correct way to use
//if var is null don't execute method() and return null

nullablevariavle!!.method()    //unsafe way
//!! -> ignore that var can be null

Decision Statements

If - Else If - Else

Kotlin
1
2
3
4
5
6
7
if (condition) {
    //code here
} else if (condition) {
    //code here
} else {
    //code here
}

Conditional Expressions

Kotlin
1
2
3
4
5
6
7
var variable: Type = if (condition) {
    //value to be assigned here
} else if (condition) {
    //value to be assigned here
} else {
    //value to be assigned here
}

When Expression

Each branch in a when expression is represented by a condition, an arrow (->), and a result.
If the condition on the left-hand side of the arrow evaluates to true, then the result of the expression on the right-hand side is returned.
Note that execution does not fall through from one branch to the next.

Kotlin
when (variable){
    condition -> value
    condition -> value
    else -> value
}

//Smart casting
when (variable){
    is Type -> value
    is Type -> value
}

//instead of chain of if-else
when {
    condition -> value
    condition -> value
    else -> value
}

Loops

For Loop

Kotlin
1
2
3
4
5
6
7
8
for (item in iterable){
    //code here
}

//loop in a numerical range
for(i in start..end) {
    //code here
}

Functions

Kotlin
1
2
3
4
5
fun functionName(parameter: Type): Type {
    //code here

    return <expression>
}

Simplifying Function Declarations

Kotlin
fun functionName(parameter: Type): Type {
    return if (condition) {
        //returned value
    } else {
        //returned value
    }
}

fun functionName(parameter: Type): Type = if (condition) {
    //returned value
    else {
        //returned value
    }
}

Anonymous Functions

Kotlin
1
2
3
4
5
val anonymousFunction: (Type) -> Type = { input ->
    //code acting on input here
}

val variableName: Type = anonymousFunction(input)

Higher-order Functions

A function can take another function as an argument. Functions that use other functions as arguments are called higher-order functions.
This pattern is useful for communicating between components in the same way that you might use a callback interface in Java.

Kotlin
1
2
3
4
fun functionName(parameter: Type, function: (Type) -> Type): Type {
    //invoke function
    return function(parameter)
}

Object Oriented Programming

Class

Kotlin
//primary constructor
class ClassName(private var attribute: Type) {

}

class ClassName {

    private var var1: Type

    //secondary constructor
    constructor(parameter: Type) {
        this.var1 = parameter
    }
}

Companion Object

Companion Object Docs

Kotlin
1
2
3
4
5
6
7
class ClassName {

    // in java: static
    companion object  {
        // static components of the class
    }
}

Collections

ArrayList

Kotlin
1
2
3
var array:ArrayList<Type>? = null  // List init

array.add(item)  //add item to list