This is a collection of short code snippets to learn Kotlin's main features from basic to advanced.
- Hello world (Go to code)
- String manipulation (Go to code)
- Variables (Go to code)
- Null Safety (Go to code)
- For example, type
String?
can be null but cannot be assigned to non-null typeString
.
- For example, type
- Arrays (Go to code)
- Conditionals (Go to code)
- Functions (Go to code)
- Classes (Go to code)
object
keyword: static members, singleton, anonymous class (Go to code)- Operator overload (Go to code)
- Define '+' or '++' for custom objects
- Java in Kotlin (Go to code), and Kotlin in Java (Go to code)
- See the referenced Java class in Kotlin, and Kotlin file in Java.
- Diverse tricks (Go to code)
- Deprecating code (Go to code)
- It is possible to deprecate a function in multiple levels, and defining an automated code for replacement.
- Using co-routines, comparing co-routines with threads (Go to code)
- Kotlin CoRoutines are very light-weight compared to java Thread.
- Domain Specific Language (DSL) style programming using higher-order functions (Go to code)
- Class property delegation (Go to code)
- This is used to intercept the setting / getting of a class property
- Using
Let
when there is concurrency (Go to code)- Uses a local copy to ensure that a variable will not be null inside a block