val constant = 10 | Immutable (final) |
var variable = 10 | Mutable |
val x: Int = 10 | Explicit type |
lateinit var name: String | Late initialization |
val lazy by lazy { } | Lazy initialization |
const val MAX = 100 | Compile-time constant |
Int, Long, Short, Byte | Integer types |
Float, Double | Floating point |
Boolean | Boolean |
Char | Character |
String | String |
Any | Root type |
Unit | Void equivalent |
Nothing | Never returns |
var name: String? | Nullable type |
name?.length | Safe call |
name ?: "default" | Elvis operator |
name!! | Not-null assertion |
name?.let { } | Safe let |
as? | Safe cast |
listOf(1, 2, 3) | Immutable list |
mutableListOf(1, 2, 3) | Mutable list |
emptyList<Int>() | Empty list |
list[0] | Get element |
list.getOrNull(0) | Safe get |
list.first() / list.last() | First/last |
list + element | Add element |
list - element | Remove element |
mapOf("a" to 1, "b" to 2) | Immutable map |
mutableMapOf<String, Int>() | Mutable map |
map["key"] | Get value (nullable) |
map.getOrDefault("key", 0) | Get with default |
setOf(1, 2, 3) | Immutable set |
mutableSetOf<Int>() | Mutable set |
.filter { it > 0 } | Filter elements |
.map { it * 2 } | Transform elements |
.flatMap { it.items } | Flatten nested |
.forEach { } | Iterate elements |
.reduce { acc, x -> acc + x } | Reduce to single |
.fold(0) { acc, x -> acc + x } | Fold with initial |
.groupBy { it.category } | Group by key |
.sortedBy { it.name } | Sort by selector |
fun name() { } | Basic function |
fun name(): Int { return 0 } | With return type |
fun name() = expression | Single expression |
fun name(x: Int = 0) | Default parameter |
fun name(vararg items: Int) | Vararg parameter |
fun name(block: () -> Unit) | Function parameter |
infix fun Int.add(x: Int) | Infix function |
tailrec fun factorial(n: Int) | Tail recursive |
{ x: Int -> x * 2 } | Lambda expression |
{ it * 2 } | Implicit it |
list.map { it * 2 } | Trailing lambda |
::functionName | Function reference |
Type::method | Method reference |
{ _, y -> y } | Unused parameter |
obj.let { it.method() } | let - returns lambda result |
obj.run { method() } | run - this as receiver |
obj.apply { prop = value } | apply - returns object |
obj.also { print(it) } | also - side effects |
with(obj) { method() } | with - no null check |
obj.takeIf { condition } | takeIf - conditional |
obj.takeUnless { condition } | takeUnless - negated |
class MyClass { } | Class definition |
class MyClass(val prop: Int) | Primary constructor |
constructor(x: Int) : this() | Secondary constructor |
init { } | Initializer block |
open class Parent | Open for inheritance |
class Child : Parent() | Inheritance |
abstract class MyClass | Abstract class |
sealed class Result | Sealed class |
data class User(val name: String) | Data class |
object Singleton { } | Object declaration |
companion object { } | Companion object |
enum class State { ACTIVE, INACTIVE } | Enum class |
value class Email(val value: String) | Value class |
inner class Inner | Inner class |
interface MyInterface { } | Interface definition |
class MyClass : Interface | Implement interface |
fun interface SAM { fun run() } | Functional interface |
class MyClass : Interface by delegate | Delegation |
var prop by Delegates.observable() | Property delegation |
suspend fun fetchData() | Suspend function |
launch { } | Fire and forget |
async { } | Returns Deferred |
deferred.await() | Await result |
runBlocking { } | Blocking coroutine |
coroutineScope { } | New scope |
withContext(Dispatchers.IO) { } | Switch context |
Dispatchers.Main | Main/UI thread |
Dispatchers.IO | I/O operations |
Dispatchers.Default | CPU-intensive |
Dispatchers.Unconfined | No specific thread |
Job() | Create job |
job.cancel() | Cancel job |
SupervisorJob() | Independent children |
flow { emit(value) } | Create flow |
flowOf(1, 2, 3) | Flow from values |
.collect { } | Collect values |
.map { } | Transform values |
.filter { } | Filter values |
.catch { } | Handle errors |
.stateIn(scope) | Convert to StateFlow |
MutableStateFlow(initial) | Mutable state flow |