Kotlin Cheat Sheet

In Kotlin

Kotlin is a modern programming language that has been gaining popularity in recent years. It was developed by JetBrains, the company behind popular IDEs like IntelliJ IDEA, and was first released in 2011. Kotlin is designed to be a more concise and expressive language than Java, while still being fully interoperable with Java code.

One of the key features of Kotlin is its focus on safety. The language includes null safety features that help prevent common programming errors, such as null pointer exceptions. Kotlin also includes support for functional programming concepts, such as lambdas and higher-order functions, which can make code more concise and easier to read.

Another advantage of Kotlin is its ease of use. The language is designed to be easy to learn and use, with a syntax that is similar to Java. Kotlin also includes a number of features that can help developers be more productive, such as type inference and extension functions.

Kotlin is also fully supported by Google for Android development, which has helped to increase its popularity in the mobile development community. In fact, Google has even made Kotlin the preferred language for Android app development.

Overall, Kotlin is a powerful and versatile programming language that is well-suited for a wide range of applications. Whether you are a seasoned developer or just starting out, Kotlin is definitely worth considering for your next project.

Basic Syntax

SyntaxDescription
fun functionName(param1: Type, param2: Type): ReturnType { }Declares a function with parameters and a return type
val variableName: Type = valueDeclares a read-only variable
var variableName: Type = valueDeclares a mutable variable
if (condition) { } else { }Conditional statement
when (value) { case1 -> { } case2 -> { } else -> { } }Switch statement
for (item in collection) { }Loop through a collection
while (condition) { }Loop while a condition is true
do { } while (condition)Loop at least once and continue while a condition is true
try { } catch (exception: Exception) { } finally { }Exception handling

Data Types

TypeDescription
BooleanRepresents a true/false value
Byte8-bit signed integer
Short16-bit signed integer
Int32-bit signed integer
Long64-bit signed integer
Float32-bit floating point number
Double64-bit floating point number
CharSingle character
StringSequence of characters

Functions

SyntaxDescription
fun functionName(param1: Type, param2: Type): ReturnType { }Declares a function with parameters and a return type
fun functionName(param1: Type = defaultValue): ReturnType { }Declares a function with default parameter values
fun functionName(vararg params: Type): ReturnType { }Declares a function with a variable number of parameters
fun functionName(lambda: (Type) -> ReturnType): ReturnType { }Declares a function that takes a lambda as a parameter

Classes

SyntaxDescription
class ClassName { }Declares a class
class ClassName(param1: Type, param2: Type) { }Declares a class with a primary constructor
class ClassName { constructor(param1: Type, param2: Type) { } }Declares a class with a secondary constructor
class ClassName { init { } }Declares a class initializer block
class ClassName { companion object { } }Declares a companion object

Properties

SyntaxDescription
val propertyName: Type = valueDeclares a read-only property
var propertyName: Type = valueDeclares a mutable property
val propertyName: Type by lazy { }Declares a lazy property
val propertyName: Type by Delegates.observable(initialValue) { property, oldValue, newValue -> }Declares a property with an observer

Extensions

SyntaxDescription
fun ClassName.extensionFunction(param1: Type, param2: Type): ReturnType { }Declares an extension function for a class
val ClassName.extensionProperty: Type get() = valueDeclares an extension property for a class

Lambdas

SyntaxDescription
{ param1: Type, param2: Type -> expression }Declares a lambda with parameters
{ param1: Type, param2: Type -> statement1; statement2 }Declares a lambda with parameters and multiple statements
{ expression }Declares a lambda with no parameters
{ statement1; statement2 }Declares a lambda with no parameters and multiple statements

Collections

SyntaxDescription
val list: List<Type> = listOf(value1, value2, value3)Declares an immutable list
val mutableList: MutableList<Type> = mutableListOf(value1, value2, value3)Declares a mutable list
val set: Set<Type> = setOf(value1, value2, value3)Declares an immutable set
val mutableSet: MutableSet<Type> = mutableSetOf(value1, value2, value3)Declares a mutable set
val map: Map<KeyType, ValueType> = mapOf(key1 to value1, key2 to value2)Declares an immutable map
val mutableMap: MutableMap<KeyType, ValueType> = mutableMapOf(key1 to value1, key2 to value2)Declares a mutable map

Null Safety

SyntaxDescription
var nullableVariable: Type? = nullDeclares a nullable variable
nullableVariable?.functionName()Calls a function on a nullable variable if it is not null
nullableVariable ?: defaultValueReturns a default value if a nullable variable is null
nullableVariable!!.functionName()Calls a function on a nullable variable and throws a NullPointerException if it is null

Coroutines

SyntaxDescription
fun main() = runBlocking { }Declares a coroutine
launch { }Launches a new coroutine
async { }Launches a new coroutine and returns a Deferred object
delay(timeInMillis)Suspends a coroutine for a specified amount of time
withContext(Dispatchers.IO) { }Switches the context of a coroutine

Conclusion

This Kotlin cheat sheet covers the basic syntax, data types, functions, classes, properties, extensions, lambdas, collections, null safety, and coroutines. Use it as a reference to help you write Kotlin code more efficiently.

Reference:

https://kotlinlang.org/docs/home.html