Slide 1

Slide 1 text

Umar Saidu Auna, @umarauna otlin: Next level of Android Development June 8, 2019

Slide 2

Slide 2 text

Who am I? I

Slide 3

Slide 3 text

otlin history

Slide 4

Slide 4 text

History Statically typed programming language, and runs on the JVM.

Slide 5

Slide 5 text

History Statically typed programming language, and runs on the JVM. Kotlin was developed by JetBrains (Makers of IntelliJ)

Slide 6

Slide 6 text

History Statically typed programming language, and runs on the JVM. Kotlin was developed by JetBrains (Makers of IntelliJ) First commit dates back to 2010, but was first publicly seen around 2011.

Slide 7

Slide 7 text

History - Why did Google make it official? Awesome features - nullable types, concise, data classes, etc. Great community support There was an overwhelming request for Kotlin official support on Android

Slide 8

Slide 8 text

otlin What?

Slide 9

Slide 9 text

What is Kotlin Kotlin is an expression-oriented language, it is also JVM based language developed by JetBrains as described before, a company known for the creation of IntelliJ IDEA, a powerful IDE for Java development. In Kotlin everything is an Object.

Slide 10

Slide 10 text

What is Kotlin Kotlin is an expression-oriented language, it is also JVM based language developed by JetBrains as described before, a company known for the creation of IntelliJ IDEA, a powerful IDE for Java development. In Kotlin everything is an Object. Kotlin is very intuitive and easy to learn for Java developers (give it 10 days trials and you wont regret).

Slide 11

Slide 11 text

What is Kotlin Kotlin is an expression-oriented language, it is also JVM based language developed by JetBrains as described before, a company known for the creation of IntelliJ IDEA, a powerful IDE for Java development. In Kotlin everything is an Object. Kotlin is very intuitive and easy to learn for Java developers (give it 10 days trials and you wont regret) . Its more expressive & safer

Slide 12

Slide 12 text

What is Kotlin Kotlin is an expression-oriented language, it is also JVM based language developed by JetBrains as described before, a company known for the creation of IntelliJ IDEA, a powerful IDE for Java development. In Kotlin everything is an Object. Kotlin is very intuitive and easy to learn for Java developers (give it 10 days trials and you wont regret). Its more expressive & safer Its highly interoperable

Slide 13

Slide 13 text

otlin interesting features

Slide 14

Slide 14 text

“In Kotlin, everything is an object (reference type), we don’t find primitives types as the ones we can use in Java……...”

Slide 15

Slide 15 text

#1 Fastest growing language

Slide 16

Slide 16 text

Companies Using Kotlin

Slide 17

Slide 17 text

Companies Using Kotlin

Slide 18

Slide 18 text

Job requirements

Slide 19

Slide 19 text

Kotlin sample code….. fun main(args: Array){ //your code goes here }

Slide 20

Slide 20 text

As of Kotlin 1.3 no more (args: Array) fun main(){ //your code goes here }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Null Safety - nullable types and non-nullable types Protects against NullPointerException the $1,000,000,000 mistake

Slide 24

Slide 24 text

Null Safety - nullable types and non-nullable types Protects against NullPointerException the $1,000,000,000 mistake // compiler error var name: String A non-nullable object can’t be null name = null

Slide 25

Slide 25 text

Null Safety - nullable types and non-nullable types Protects against NullPointerException the $1,000,000,000 mistake // compiler error var name: String A non-nullable object can’t be null name = null // compiler error Specify a nullable object by using “?” var name: String? val length = name.length Kotlin ensures that you don’t mistakenly operate on nullable objects

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Null Safety - Accessing properties in a nullable object 1. Checking for null types // handle non-null case and null case var name: String? = null val length = if (name != null) name.length else 0

Slide 28

Slide 28 text

Null Safety - Accessing properties in a nullable object 2. Making safe calls using “?.” //use ?. to make safe call var name: String? ... val length = name?.length Use ?. to safely access a property/method on a nullable object If name happens to be null, the value of length is 0 (inferred integer). length would be null if it were of another type.

Slide 29

Slide 29 text

Null Safety - Accessing properties in a nullable object 3. Making use of the “?:” elvis operator //use elvis operator var name: String? val length = name?.length ?: 0 This reads as “if name is not null, use name.length else use 0

Slide 30

Slide 30 text

Null Safety - Accessing properties in a nullable object 4. Making use of the “!!” assertion operator //use elvis operator var name: String? = null val length = name!!.length This reads as “if name is not null, use name.length else throw a null pointer exception”

Slide 31

Slide 31 text

Null Safety - Accessing properties in a nullable object 4. Making use of the “!!” assertion operator //use elvis operator var name: String? = null val length = name!!.length This reads as “if name is not null, use name.length else throw a null pointer exception” Be very careful with this operator!!!

Slide 32

Slide 32 text

Null Safety - NPE free! You can only have the NullPointerException in Kotlin if: 1. You explicitly throw a NPE 2. You make use of the !! operator 3. An external Java code causes the NPE.

Slide 33

Slide 33 text

String Interpolation in Kotlin fun hello(name: String) { println("Hello, $name") }

Slide 34

Slide 34 text

String Interpolation in Java + Kotlin void hello(String name) { System.out.println("Hello, " + name); }

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Immutability Kotlin helps developers to be intentional about immutability. Immutability simply means that things you create can’t be changed. We need immutability because it: ● helps us with thread safety - no synchronization issues ● is good for value objects - objects that simply hold value, POJOs etc. ● helps debugging threaded applications without losing your hair

Slide 37

Slide 37 text

Immutability How does it work in Java? ● Immutability in Java? final classes private fields no setters ● Immutability in Kotlin? Guess?

Slide 38

Slide 38 text

Immutability How does Kotlin help? var vs val // compiler error: val cannot be reassigned val name = "Umar" name = name.toUpperCase() // works fine var name = "Umar" name = name.toUpperCase()

Slide 39

Slide 39 text

Immutability How does Kotlin help? Immutable and Mutable collections // immutable collection val unchangeableHobbies = listOf("coding", "eating") unchangeableHobbies.add() // add method doesn’t exist // mutable collection val changeableHobbies = mutableListOf("reading", "running") changeableHobbies.add("volleyball") // you can add

Slide 40

Slide 40 text

Immutability In Java vs Kotlin public final class ImmutableClassJava { private final String name; private final int age; public ImmutableClassJava(String name, int age) { this.name = name; this.age = age; } // no setters public String getName() { return name; } public int getAge() { return age; } } class ImmutableClass(val name: String, val age: Int, val grade: Char, val hobbies: List) ● Class is final by default ● val implies that the parameters are final as well (values can’t be assigned)

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Functions // function sample fun sampleFunc() { // code goes here } A function is declared using the “fun” keyword

Slide 43

Slide 43 text

Functions // function sample fun sampleFunc() { // code goes here } // function with param fun sampleFuncWithParam(param: String) { // code goes here } A function is declared using the “fun” keyword Method parameters use the “name:Type” notation

Slide 44

Slide 44 text

Functions // function sample fun sampleFunc() { // code goes here } // function with param fun sampleFuncWithParam(param: String) { // code goes here } // func with param and return type fun capitalize(param: String): String { return param.toUpperCase() } A function is declared using the “fun” keyword Method parameters use the “name:Type” notation Return types are specified after the method definition.

Slide 45

Slide 45 text

Functions - infix functions // infix function infix fun Int.times(x: Int): Int { return this * x }

Slide 46

Slide 46 text

Functions - infix functions // infix function infix fun Int.times(x: Int): Int { return this * x } // usage fun useInfix() { val product = 2 times 5 println(product) }

Slide 47

Slide 47 text

Functions - extension functions // extension function fun Int.square(): Int { return this * this }

Slide 48

Slide 48 text

Functions - extension functions // extension function fun Int.square(): Int { return this * this } fun useExtension() { val square = 2.square() println(square) }

Slide 49

Slide 49 text

Functions Others: ● Higher order functions ● Lambdas ● Inline functions etc.

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Iterators Java Kotlin

Slide 52

Slide 52 text

When Expression

Slide 53

Slide 53 text

otlin conciseness

Slide 54

Slide 54 text

Conciseness in Kotlin public void doSomething() { // do something } fun doSomething(): Unit { // do same thing }

Slide 55

Slide 55 text

Conciseness in Kotlin public void doSomething() { // do something } fun doSomething(): Unit { // do same thing }

Slide 56

Slide 56 text

Conciseness in Kotlin public void doSomething() { // do something } fun doSomething() { // do same thing }

Slide 57

Slide 57 text

Conciseness in Kotlin public String getName() { return name; } fun getName(): String { return name }

Slide 58

Slide 58 text

Conciseness in Kotlin public String getName() { return name; } fun getName() { return name }

Slide 59

Slide 59 text

Conciseness in Kotlin public String getName() { return name; } fun getName() = name

Slide 60

Slide 60 text

otlin classes

Slide 61

Slide 61 text

Classes in Java vs Data Classes in Kotlin public class Person { private String name; String getName () { return name; } void setName (String name) { this.name = name; } @Override public String toString () { return "Person{" + "name='" + name + '\'' + '}'; } } data class Person(val name: String)

Slide 62

Slide 62 text

Classes in Java vs Regular Classes in Kotlin public class Person { private String name; String getName () { return name; } void setName (String name) { this.name = name; } public int getNameLength () { return name.length } } class Person(name: String) { var name: String get() = name set(name) { this.name = name } fun getNameLength(): int { return name.length } }

Slide 63

Slide 63 text

Classes in Java vs Regular Classes in Kotlin public class Person { private String name; String getName () { return name; } void setName (String name) { this.name = name; } public int getNameLength () { return name.length } } class Person(name: String) { var name: String get() = name set(name) { this.name = name } fun getNameLength() = name.length }

Slide 64

Slide 64 text

otlin: getting started

Slide 65

Slide 65 text

Important place to learn…….

Slide 66

Slide 66 text

Important place to learn……. Tutorials Point - Kotlin

Slide 67

Slide 67 text

Important place to learn……. Tutorials Point - Kotlin Kotlin for Android Developers – Antonio Leiva

Slide 68

Slide 68 text

Important place to learn……. Tutorials Point - Kotlin Kotlin for Android Developers – Antonio Leiva Koans online – try.kotl.in/koans

Slide 69

Slide 69 text

Important place to learn……. Tutorials Point - Kotlin Kotlin for Android Developers – Antonio Leiva Koans online – try.kotl.in/koans Kotlin Bootcamp for Programmers - Udacity

Slide 70

Slide 70 text

Compilations

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

IDE’s

Slide 73

Slide 73 text

Build Tools

Slide 74

Slide 74 text

Stay updated……. Kotlin Weekly http://www.kotlinweekly.net/ Kotlin Conf kotlinconf.com

Slide 75

Slide 75 text

Summary • Interoperable with Java (100%) • Reduces Boilerplate code • Object-Oriented and procedural • Safety code • No Semicolon • Expands your skillset • Perfect Support with Android Studio & Gradle • Very easy to get started with Android Development

Slide 76

Slide 76 text

otlin: libraries

Slide 77

Slide 77 text

Reduces Boilerplates codes like: findViewById, Async, build interfaces with Kotlin code etc

Slide 78

Slide 78 text

Kotlin Android Extension it includes a view binder. The plugin automatically creates a set of properties that give direct access to all the views in the XML.

Slide 79

Slide 79 text

Async Server - Client

Slide 80

Slide 80 text

https://kotlinlang.org/user-groups/user-group-list.html @kanokotlin

Slide 81

Slide 81 text

Finally 1 You have 3 Options.. Decide Kotlin is not For you and continue with Java

Slide 82

Slide 82 text

Finally 1 You have 3 Options.. Decide Kotlin is not For you and continue with Java Try to learn everything on your own 2

Slide 83

Slide 83 text

Finally 1 You have 3 Options.. Decide Kotlin is not For you and continue with Java 3 Go and learn on MOOC websites Try to learn everything on your own 2

Slide 84

Slide 84 text

Thank you! @umarauna @android200