June 2018
Intermediate to advanced
316 pages
6h 34m
English
A closure is also known as a capturing lambda because it captures a variable from outside. Java doesn't allow captured local variables to be modified inside an inner-anonymous class or a lambda. This is because the lambda or instance of the inner-anonymous class can be passed to another thread, and if two threads can modify the same variable this can lead to a race condition. This restriction prevents multithreading issues. Kotlin allows you to modify a local variable inside a lambda:
fun main(args: Array<String>) { var counter = 0 val inc = {
counter ++ } inc()}
The decompiled Java code looks like this:
public static final void main(@NotNull String[] args) { Intrinsics.checkParameterIsNotNull(args,
Read now
Unlock full access