Slide 1

Slide 1 text

Why you should consider using it? @soulesidibe

Slide 2

Slide 2 text

• Quick introduction to Kotlin • Features that I love • Kotlin for Android developers • What else? Agenda

Slide 3

Slide 3 text

Quick introduction to Kotlin

Slide 4

Slide 4 text

• Created by JetBrains • Statically-typed programming language • For the JVM

Slide 5

Slide 5 text

Java interoperability • 100% interoperable with Java • Your framework and libs are available

Slide 6

Slide 6 text

String interpolation val x = 4 val y = 7 print("sum of $x and $y is ${x + y}”) "// sum of 4 and 7 is 11

Slide 7

Slide 7 text

Type inference val a = "abc" "// type inferred to String val b = 4 "// type inferred to Int val c: Double = 0.7 "// type declared explicitly val d: List = ArrayList() "// type declared explicitly

Slide 8

Slide 8 text

Default Arguments fun build(title: String, width: Int = 800, height: Int = 600) { Frame(title, width, height) } fun main(args: Array) { build("Title") build("Title", 1024, 1024) build("Title", height = 1024) }

Slide 9

Slide 9 text

Data Class data class Person(val name: String, var email: String, var age: Int) val person = Person("Abdou", "[email protected]", 20) "//Person(name=Abdou, [email protected], age=20)

Slide 10

Slide 10 text

Better for loop for (i in 1"..100) { ""... } for (i in 0 until 100) { ""... } for (i in 2"..10 step 2) { ""... } for (i in 10 downTo 1) { ""... }

Slide 11

Slide 11 text

Better collections api val list: List = listOf(1, 2, 3, 4) mapOf("Mouhamed" to 21, "ali" to 25, "Papi" to 29) setOf("one", "two", "three") val mutableList: MutableList = 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 }

Slide 12

Slide 12 text

Features that I love

Slide 13

Slide 13 text

Null safety • NullPointerException Problem • Nullable Type, Non Nullable Type fun main(args: Array) { var name: String = “Soulesidibe" }

Slide 14

Slide 14 text

Null safety • NullPointerException Problem • Nullable Type, Non Nullable Type fun main(args: Array) { var name: String? = “Soulesidibe" }

Slide 15

Slide 15 text

Null safety • NullPointerException Problem • Nullable Type, Non Nullable Type fun main(args: Array) { var name: String? = “Soulesidibe” val length = name?.length }

Slide 16

Slide 16 text

Null safety • NullPointerException Problem • Nullable Type, Non Nullable Type fun main(args: Array) { var name: String? = “Soulesidibe” val length = if (name "== null) 0 else name.length }

Slide 17

Slide 17 text

Extension functions • Extend a class without modifying it • Avoid utils classes fun String.hello(): String { return “Hello $this” } fun main(args: Array) { var name: String = “Soulesidibe” println(name.hello()) }

Slide 18

Slide 18 text

Extension functions • Extend a class without modifying it • Avoid utils classes fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] this[index1] = this[index2] this[index2] = tmp } fun main(args: Array) { var list = mutableListOf(1, 2, 3, 5) list.swap(1,2) "//1,3,2,5 }

Slide 19

Slide 19 text

High order functions • A function that takes a function 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) { operation(3, 4, {a, b "-> a + b}) }

Slide 20

Slide 20 text

High order functions • A function that takes a function 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) { operation(3, 4) {a, b "-> a + b} }

Slide 21

Slide 21 text

Class Delegation •Design pattern in OOP •Object composition without inheritance •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) { Derived(10).print() "// prints 10 }

Slide 22

Slide 22 text

Class Delegation •Design pattern in OOP •Object composition without inheritance •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) { val b = BaseImpl(10) Derived(b).print() "// prints 10 }

Slide 23

Slide 23 text

Kotlin for android developers

Slide 24

Slide 24 text

Kotlin for android developers • Google announces official support on android • Android Studio supports Kotlin • Jake Wharton joins Google • The community adopts Kotlin

Slide 25

Slide 25 text

Interoperability • 100% compatible with java 6, 7, 8, 9 • Kotlin and Java interoperable class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }

Slide 26

Slide 26 text

Be lazy • Value gets computed only upon first access • Better memory management(Heap) • Less garbage collection class MainActivity : AppCompatActivity() { val button: Button by lazy { findViewById(R.id.btn_click) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.setOnClickListerner{ "// code… } } }

Slide 27

Slide 27 text

Extension functions • Avoid API version check • More concise code class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) if (button.isAttachedWindow()) { "// coded } } } fun View.isAttachedWindow(): Boolean { return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { ViewCompat.isAttachedToWindow(this) } else isAttachedToWindow }

Slide 28

Slide 28 text

Extension functions • Avoid boilerplate • More concise code "// java String firstName = null; int firsNameColumn = cursor.getColumnIndexOrThrow("first_name"); if (!cursor.isNull(firsNameColumn)) { firstName = cursor.getString(firsNameColumn); }

Slide 29

Slide 29 text

Extension functions • Avoid boilerplate • More concise code "// 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) }

Slide 30

Slide 30 text

Extension functions • Avoid boilerplate • More concise code "// 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"

Slide 31

Slide 31 text

What else?

Slide 32

Slide 32 text

What else? • Not just for android developers • Kotlin for server side(Spring, Ktor, Vertx, Spark) • Kotlin/Js (client and server) • Kotlin/native "=> Code sharing

Slide 33

Slide 33 text

What else?

Slide 34

Slide 34 text

Where to learn Kotlin • Official Kotlin Doc • Jake Wharton talk • Antonio Leiva Blog • My blog • Try it online

Slide 35

Slide 35 text

That’s it! @soulesidibe