Slide 1

Slide 1 text

{ Kotlin } Statically typed language for the JVM, Android and the browser. Developed by JetBrains First stable release February 15, 2016.

Slide 2

Slide 2 text

{ Syntax }. var and val var : = [property initializer] get() // optional set() // optional //example var str: String = “kotlin is awesome" OR var str = “kotlin is awesome"

Slide 3

Slide 3 text

{ Syntax }. method declaration fun : //example fun saySomthing() : String { return “kotlin is awesome" } OR fun saySomthing() = “kotlin is awesome"

Slide 4

Slide 4 text

{ Syntax } when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") }

Slide 5

Slide 5 text

{ Syntax } for ((k, v) in map) { println("$k -> $v") } someObject?.let { // execute this block if not null } fun foo(param: Int) { val result = if (param == 1) { "one" } else if (param == 2) { "two" } else { "three" } }

Slide 6

Slide 6 text

{ Syntax } .inheritance all classes by default are finals open class Base { open fun v() {} fun nv() {} } class Derived() : Base() { override fun v() {} }

Slide 7

Slide 7 text

{ Syntax } .inheritance open class A { open fun f() { print("A") } fun a() { print("a") } } interface B { fun f() { print("B") } fun b() { print("b") } } class C() : A(), B { // The compiler requires f() to be overridden: override fun f() { super.f() // call to A.f() super.f() // call to B.f() } }

Slide 8

Slide 8 text

{ Syntax } .inner classes class A { // implicit label @A inner class B { // implicit label @B fun foo() { val a = this@A // A's this val b = this@B // B's this } } }

Slide 9

Slide 9 text

Billion-dollar mistake Tony Hoare My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement.

Slide 10

Slide 10 text

{ null safety } nullable and non-nullable types var str: String = “kotlin is awesome" str = null // compilation error… str is non-nullable type var str: String? = “kotlin is awesome" str = null // ok... str now can be null val length = name.length // compilation error val length = name?.length // ok

Slide 11

Slide 11 text

{ null safety } nullable and non-nullable types var name: String? = “some string" if (name != null){ length = name.length }else{ length = 0 } or more shortly val length = name?.length ?: 0

Slide 12

Slide 12 text

{ Default parameters } fun tmpMethod(a: Int = -10, b: Int = 5, c: Int = 0){ print(a + b + c) } ---------------------------------------------------------------- tmpMethod(1) // 7 tmpMethod(2) // -8 tmpMethod(1,2,3) // 6 tmpMethod(b = 0, a = 2) // 2

Slide 13

Slide 13 text

{ Delegated Properties } Lazy & Observable

Slide 14

Slide 14 text

{ Delegated Properties } val lazyValue: String by lazy { println("computed!") "Hello" } fun main(args: Array) { println(lazyValue) println(lazyValue) } This example prints: computed! Hello Hello

Slide 15

Slide 15 text

{ Delegated Properties } var age: Int by Delegates.observable(0) { prop, old, new -> println("$old -> $new") } age = 10 // 0 -> 10 age = 11 // 10 -> 11

Slide 16

Slide 16 text

{ No more Utils }.utilHell

Slide 17

Slide 17 text

{ Extensions } Extension methods & Extension properties

Slide 18

Slide 18 text

{ Extensions }.methods val nullableString : String? = null if (nullableString.isNullOrEmpty()){ // nullableString is null or empty }

Slide 19

Slide 19 text

{ Extensions }.methods var myObject : MyClass? = null if (myObject.isNull()){ // my object is null }

Slide 20

Slide 20 text

{ Extensions }.methods var myObject : MyClass? = null if (myObject.isNull()){ // my object is null } fun MyClass?.isNull() : Boolean { return this == null } OR fun MyClass?.isNull() = this == null

Slide 21

Slide 21 text

{ Extensions }.methods fun Activity.toast(msg: String, toastLength: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, msg, toastLength).show() } fun Fragment.toast(msg: String, toastLength: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, msg, toastLength).show() }

Slide 22

Slide 22 text

{ Extensions }.properties val myList = listOf(1,2,3,4,5) val lastIndex = myList.lastIndex // lastIndex = 4 --------------------------------

Slide 23

Slide 23 text

{ Extensions }.properties val myList = listOf(1,2,3,4,5) val lastIndex = myList.lastIndex // lastIndex = 4 -------------------------------- val List.lastIndex: Int get() = size - 1

Slide 24

Slide 24 text

{ Lambdas } Collections.sort(items, { t1, t2 -> t1.name.compareTo(t2.name)}) --------------------------------------------------------------------------------------------------------- in java Collections.sort(items, new Comparator() { @Override public int compare(MyClass t1, MyClass t2) { return t1.getName().compareTo(t2.getName()); } });

Slide 25

Slide 25 text

{ Higher-Order funcitons} fun List.map(transform: (T) -> R): List { val result = arrayListOf() for (item in this) result.add(transform(item)) return result } val doubled = items.map { it * 2 } fun max(collection: Collection, less: (T, T) -> Boolean): T? { var max: T? = null for (it in collection) if (max == null || less(max, it)) max = it return max } val less = {a:Int,b:Int -> a > b}

Slide 26

Slide 26 text

Example of SqliteDatabase transaction in android ------------------------------------------------------------------------------------ db.beginTransaction() try { db.delete("tableName","firstName =?", arrayOf("Jake")) db.setTransactionSuccessful() }finally { db.endTransaction() }

Slide 27

Slide 27 text

Example of SqliteDatabase transaction in android ------------------------------------------------------------------------------------ db.beginTransaction() try { // do work here db.setTransactionSuccessful() }finally { db.endTransaction() }

Slide 28

Slide 28 text

{ Extension Function Expression } db.inTransaction { delete("tableName","firstName =?", arrayOf("Jake")) }

Slide 29

Slide 29 text

{ Extension Function Expression } fun SQLiteDatabase.inTransaction(func: () -> Unit) { beginTransaction() try { func() setTransactionSuccessful() }finally { endTransaction() } } db.inTransaction { db.delete("tableName","firstName =?", arrayOf("Jake")) }

Slide 30

Slide 30 text

{ Extension Function Expression } fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try { func() setTransactionSuccessful() }finally { endTransaction() } } db.inTransaction { delete("tableName","firstName =?", arrayOf("Jake")) }

Slide 31

Slide 31 text

{ Kotlin in Android } // in java Button button = (Button) findViewById(R.id.my_button_id); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show(); } }); // in kotlin val button = findViewById(R.id.my_button_id) as Button button.setOnClickListener { toast("clicked") }

Slide 32

Slide 32 text

{ Kotlin in Android } // JAVA button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { button.getViewTreeObserver().removeOnGlobalLayoutListener(this); Toast.makeText(Main2Activity.this, button.getHeight() +"", Toast.LENGTH_SHORT).show(); } }); kotlin any_type_view.afterMeasured { toast(any_type_view.height.toString()) }

Slide 33

Slide 33 text

{ Extension Function Expression } inline fun View.afterMeasured(crossinline f: View.() -> Unit) { viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { if (measuredWidth > 0 && measuredHeight > 0) { viewTreeObserver.removeOnGlobalLayoutListener(this) f() } } }) }

Slide 34

Slide 34 text

{ Kotlin in Android } // in java button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show(); } }); // in kotlin button.setOnClickListener { toast("clicked") }

Slide 35

Slide 35 text

{ Kotlin in Android }. no more findViewById Syntetic import apply plugin: 'kotlin-android-extensions' my_button_id.setOnClickListener { } my_text_view_id.text = "some text"

Slide 36

Slide 36 text

{ Kotlin in Android }.Anko library

Slide 37

Slide 37 text

{ Kotlin in Android }.Anko library async() { // Do something in a secondary thread uiThread { // Back to the main thread } }

Slide 38

Slide 38 text

{ Performance } ???

Slide 39

Slide 39 text

{ As fast as Java } thanks ;)

Slide 40

Slide 40 text

https://kotlinlang.org/docs/reference/ http://antonioleiva.com/kotlin/ https://medium.com/keepsafe-engineering/kotlin-vs-java-compilation-speed-e6c174b39b5d#.8w0o4sh91

Slide 41

Slide 41 text

with kotlin you get ● lambdas ● default methods ● method references ● extension lambdas ● nullable types ● null safety ● inlined methods (which reduce the cost of lambdas immensely) ● extension methods ● improved type inference ● operator overloading (limited) ● infix methods ● reified generics for methods ● data classes ● property delegation ● interface delegation