String val b = 4 "// type inferred to Int val c: Double = 0.7 "// type declared explicitly val d: List<String> = ArrayList() "// type declared explicitly
4) mapOf<String, Int>("Mouhamed" to 21, "ali" to 25, "Papi" to 29) setOf<String>("one", "two", "three") val mutableList: MutableList<Int> = mutableListOf(1, 2, 3, 4) mutableMapOf("Mouhamed" to 21, "ali" to 25, "Papi" to 29) mutableSetOf("one", "two", "three") val list = listOf(1, 2, 3, 4) val newList = list.map { it * 2 }.filter { it > 2 }
as parameter • A function that returns a function fun operation(a: Int, b: Int, func: (Int,Int) "-> Int): Int { return func(a, b) } fun main(args: Array<String>) { operation(3, 4, {a, b "-> a + b}) }
as parameter • A function that returns a function fun operation(a: Int, b: Int, func: (Int,Int) "-> Int): Int { return func(a, b) } fun main(args: Array<String>) { operation(3, 4) {a, b "-> a + b} }
•Native support on Kotlin interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(val x: Int) : Base by BaseImpl(x) fun main(args: Array<String>) { Derived(10).print() "// prints 10 }
•Native support on Kotlin interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b fun main(args: Array<String>) { val b = BaseImpl(10) Derived(b).print() "// prints 10 }
java String firstName = null; int firsNameColumn = cursor.getColumnIndexOrThrow("first_name"); if (!cursor.isNull(firsNameColumn)) { firstName = cursor.getString(firsNameColumn); } "// kotlin fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return if (isNull(index)) null else getString(index) } val name = cursor.getStringOrNull("first_name") firstName.text = name "?: "Soulesidibe"