a: Int = 1 // val = READ ONLY (getter) var b: Int = 1 // var = READ/WRITE (getter/setter) • String templates "My name is $name $surname" • Lambdas view.setOnClickListener { Log.d("TAG", "Item clicked!") } • Delegated properties (example: lazy) val item by lazy { MyItem() }
(getter/setter) val onlyRead = 1 var writable = 2 var nullable: Int? = 3 fun test() { onlyRead = 3 // Error at compile time writable = 3 writable = "test" // Error at compile time writable = null // Error at compile time nullable = null } // Customize getter/setter val p1: Int = 1 get() { // add some logic return field } var p2: Int = 2 get() { return field } set(value) { // add some logic field = value } Properties // More examples
str = "My name is $name" str = "My name is $name (${name.length} chars)" str = "$name == ${name}" str = "$name.length != ${name.length}" // IS NOT THE SAME str = "Time is: ${System.currentTimeMillis()}ms since 1970" } String templates // More examples
Delegates.observable(3) { prop, old, new -> if (new > old) Log.w("TEST", "$new > $old") } fun test() { startTime test = 5 test = 1 test = 2 val finish = System.currentTimeMillis() - startTime } Delegated properties // More examples
var a: String = null // Error at compile time var b: String? = null • Elvis Operator val example: String = b ?: "Default" // b may be null • Smart-cast if (myView is TextView) { myView.setText("Ciao") } • Collections listOf("Android", "iOS", null).filterNotNull().map { it.toUpperCase() }
"new string" test.length // Error at compile time test?.length // if (test != null) test.length else null test!!.length // test MUST exist or throw NullPointerException val len = test?.length ?: 0 // default value = 0 test?.length ?: throw IllegalStateException("Missing the main string!") } Null safety & Elvis Operator // More examples
val _test: Any? = test if (_test != null) { val checkEqual = _test.equals("My new value") if (_test is String) { val checkLen = _test.length _test.capitalize() _test.substring(1) } } when (_test) { is Int -> { val sum = _test + 1 } is String -> { val len = _test.length } } } Smart-cast // More examples
{ return if (power > opponent.power) { this } else { opponent } } } fun example() { val thor = Hero(7) val ironman = Hero(8) val spiderman = Hero(4) } // Skip Java version of Hero class, // we use Kotlin class class MyJavaClass { void example() { Hero thor = new Hero(7); Hero ironman = new Hero(8); Hero spiderman = new Hero(4); } } #10 Kotlin - Infix Notation http://kotlinlang.org/docs/reference/functions.html#infix-notation vs
{ return if (power > opponent.power) { this } else { opponent } } } fun example() { val thor = Hero(7) val ironman = Hero(8) val spiderman = Hero(4) val theBest = thor vs ironman vs spiderman } // Skip Java version of Hero class, // we use Kotlin class class MyJavaClass { void example() { Hero thor = new Hero(7); Hero ironman = new Hero(8); Hero spiderman = new Hero(4); Hero theBest = thor.vs(ironman).vs(spiderman); } } #10 Kotlin - Infix Notation http://kotlinlang.org/docs/reference/functions.html#infix-notation vs
{ return if (power > opponent.power) { this } else { opponent } } } fun example() { val thor = Hero(7) val ironman = Hero(8) val spiderman = Hero(4) val theBest = thor vs ironman vs spiderman } // Skip Java version of Hero class, // we use Kotlin class class MyJavaClass { void example() { Hero thor = new Hero(7); Hero ironman = new Hero(8); Hero spiderman = new Hero(4); Hero theBest = thor.vs(ironman).vs(spiderman); } } #10 Kotlin - Infix Notation http://kotlinlang.org/docs/reference/functions.html#infix-notation vs
eyes: String, val hair: String) // Skip Java version of Male, Female and Baby // class, there is not enough space! #11 Kotlin - Operator Overloading http://kotlinlang.org/docs/reference/operator-overloading.html vs
{ return Baby(this.eyes, her.hair) } } class Female(val hair: String) class Baby(val eyes: String, val hair: String) // Skip Java version of Male, Female and Baby // class, there is not enough space! #11 Kotlin - Operator Overloading http://kotlinlang.org/docs/reference/operator-overloading.html vs
{ return Baby(this.eyes, her.hair) } } class Female(val hair: String) class Baby(val eyes: String, val hair: String) fun example() { val myBaby = Male("green") + Female("blond") } // Skip Java version of Male, Female and Baby // class, there is not enough space! class MyJavaClass { void example() { Baby myBaby = new Male("green").plus( new Female("blond")); } } #11 Kotlin - Operator Overloading http://kotlinlang.org/docs/reference/operator-overloading.html vs
{ return Baby(this.eyes, her.hair) } } class Female(val hair: String) class Baby(val eyes: String, val hair: String) fun example() { val myBaby = Male("green") + Female("blond") } // Skip Java version of Male, Female and Baby // class, there is not enough space! class MyJavaClass { void example() { Baby myBaby = new Male("green").plus(new Female("blond")); } } #11 Kotlin - Operator Overloading http://kotlinlang.org/docs/reference/operator-overloading.html vs
= context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(windowToken, 0) } /** * Kotlin version of method Utils.isViewInBounds(container, view) by Alex Lockwood * URL: https://github.com/alexjlockwood/activity-transitions/blob/c4fa3a21c941691fb6bbe53e37cf3965842ee254/app/src/ main/java/com/alexjlockwood/activity/transitions/Utils.java * * Returns true if {@param view} is contained within {@param container}'s bounds. */ fun View.isInBounds(container: View): Boolean { val containerBounds = Rect() container.getHitRect(containerBounds) return getLocalVisibleRect(containerBounds) }
String): String = if (!this.isNullOrBlank()) this!! else defaultValue infix fun String?.or(defaultValue: String?): String? = if (!this.isNullOrBlank()) this else defaultValue // val a: String? = null // val b: String? = null // val c: String? = "test" // val myText = a or b or c orNotNull "default"