Kotlin

Language Overview

Class

Similar to Java class

class Person(val name: String, val age: Int) // concise constructor

// Full primary constructor
class Person(name: String) {
    init {
        this.name = name
    }
}

// Changing visibility
class InternalComponent
internal constructor(name: String) {
    ...
}

// Secondary constructor
class Rectangle(val height: Int, val width: Int) {
    constructor(side: Int) : this(side, side) { ... }
}

Class modifiers

Class Delegation

Data class

Similar to class, but also includes definition for copy, equals, hashCode and toString methods

Enum class

Sealed class

Restricts the class hierarchy: all subclasses must be located in the same file

Conditionals

If

If is an expression in Kotlin - it returns a value you can assign to a variable

Ternary operator

There is no ternary operator in Kotlin

When

Similar to Java's switch statement, useful with many conditions

Constants

Compile-time constants

The value will be inlined everywhere in the resulting bytecode. It's possible only for primitive types and String.

@JvmField

Exposes a Kotlin property as a field in Java, so no getter and setter are generated.

Exceptions

There is no difference between checked and unchecked exceptions

Try is an expression

Functions

Default arguments

Extension functions

They are defined outside of the class, but they can be called as a regular member of the class. They have to be imported in all the locations where the function is used. As they are static Java methods under the hood, no override is possible for extension functions.

Infix functions

Inline functions

The compiler will substitute the body of an inline function instead of calling it, which makes the performance overhead for inline functions to zero.

Named arguments

Hello, world!

Lambdas

They replace anonymous classes in Java, and allow functional programming

Lambda with receiver

Similar to an extensions function, it has an implicit this set to the first argument of the with extension function

Lateinit

lateinit allows non-nullable properties to be initialized outside the constructor method. It cannot be a val, because that would violate immutability, and cannot be nullable or be a primitive type, as it's initialized to null under the hood. We can check if the property has been inialized by using the isinitialized property.

Loops

For

While

Main with arguments

Multiline strings

Nullability

The modern approach is to make a NPE a compile-time error rather than a run-time error

Object

It's the Kotlin way to express a Singleton

Companion object

Special object inside a class, useful to implement static methods in Kotlin. They can also implement a interface.

Object expression

Replaces anonymous classes with multiple methods (if the anonymous class has a single method, it can be replaced by a lambda expresssion). This is not a singleton.

Properties

Ranges

Sequences

Allow lazy operations on collections without creating intermediate collections. They will apply "vertical" evaluation (the first element goes through the whole chain, then the second, etc.) rather than the "horizontal" evaluation collections will perform. Nothing happens until there is a terminal operation.

String templates

Variables

Resources

Articles

Awesome

Courses

Last updated

Was this helpful?