= "John" final String name = "John"; private private String name = "John" public String getName() { return name; } public void setName(String name){ this.name = name; } name var = "John" user.name = "Jane" val tempName = user.name
fun click() = println("I'm clicked") override fun tap(){ if(touchEvent.duration in 1..100){ //do something } } } else { click() } val area: Area override val area: Area = Area(10, 40) { Basics
2") 4 -> println("Correct result is 4") else -> { println("Wrong result is $x") } } when(x) { in 1..10 -> println("Result is in safe range") 11, 12 -> println("Result is 11 or 12") else -> { println("Result $x is out of range") } }
people's classes • We're good as long as it is compiled to Java class fun String.lastChar(): Char = this.get(this.length - 1) Receiver type Function name Receiver object fun String.lastChar(): Char = get(length - 1) import some_package_name.lastChar println("John".lastChar()) val String.lastChar: Char = get() = get(length - 1) println("John".lastChar)
argument or returns one • Function types val sum: (Int, Int) -> Int = { x, y -> x + y } Parameter Types Return Type val sum = { x: Int, y: Int -> x + y } Int
-> x + y } val div = { x: Int, y: Int -> x / y } val mul = { x: Int, y: Int -> x * y } fun <T : Number> execute(opOne: T, opTwo: T, opFn: (T, T) -> T) = opFn(opOne, opTwo) val sumRes = execute(2, 3, sum) val multiplicationResult = execute(2, 3, mul) val divideResult = execute(2, 3, div)