== 1 or x == 2”); break; default: System.out.println("x is neither 1 nor 2"); } // Kotlin when (x) { 1..2 -> println("x == 1 or x == 2") else -> println("x is neither 1 nor 2") }
for (i in 1..length) { print(character) } } drawHRLine('*') drawHRLine() drawHRLine(12) // Compile Error! Cannot do this drawHRLine('=', 12) // Does not seem real nice drawHRLine(length = 12) // Named argument
-> x + y } val sum = { x: Int, y: Int -> x + y } val notEmpty: (TextView) -> Boolean = { it.text.isNotEmpty() } val notEmpty = { textView: TextView -> textView.text.isNotEmpty() } Lamda Expressions
not member functions • Do not modify the original class in any way • Are resolved at compile time by static type information • Are compiled to static functions in Java
destination = ArrayList<R>() for (item in this) { destination.add(predicate(item)) } return destination } Higher Order Function val original = arrayListOf(2, 4, 6, 8) original.map { it + 2 } // 4, 6, 8, 10