Slide 1

Slide 1 text

Kotlin The good Moyinoluwa Adeyemi

Slide 2

Slide 2 text

Kotlin The good, the bad and the ugly Segun Famisa Moyinoluwa Adeyemi

Slide 3

Slide 3 text

Intro to Kotlin - History Statically typed programming language, and runs on the JVM.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Intro to Kotlin - 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 6

Slide 6 text

Intro to Kotlin - 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 7

Slide 7 text

Interesting Features of Kotlin

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Null Safety - Accessing properties in a nullable object // use ?. to make safe call var name: String? = null ... 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. 2. Making safe calls using “?.”

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Null Safety - Accessing properties in a nullable object // use !! assertion 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” 4. Making use of the “!!” assertion operator

Slide 15

Slide 15 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 16

Slide 16 text

Smart inference // you don’t need to explicitly put a type val name = "Segun" // string val age = 24 // int val grade = 'A' // char val hobbies = listOf("karate", "eating") // list of strings Kotlin can smartly infer the type of your variable from the data it contains

Slide 17

Slide 17 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 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 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("karate", "basketball") changeableHobbies.add("soccer") // you can add

Slide 21

Slide 21 text

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) Immutability in Java vs Kotlin

Slide 22

Slide 22 text

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

Slide 23

Slide 23 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 24

Slide 24 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() } Method parameters use the “name:Type” notation Return types are specified after the method definition. A function is declared using the “fun” keyword

Slide 25

Slide 25 text

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

Slide 26

Slide 26 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 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Conciseness

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Classes

Slide 38

Slide 38 text

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) Classes in Java vs Data Classes in Kotlin

Slide 39

Slide 39 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(var name: String) { val nameLength: Int get() = name.length }

Slide 40

Slide 40 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(var name: String) { val nameLength get() = name.length }

Slide 41

Slide 41 text

Getting started

Slide 42

Slide 42 text

Kotlin Koans ● Koans online

Slide 43

Slide 43 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin

Slide 44

Slide 44 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin ● Clone the project on Github

Slide 45

Slide 45 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin ● Clone the project on Github IDE support Kotlin Koans + plugin

Slide 46

Slide 46 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin ● Clone the project on Github IDE support = built-in support + plugin

Slide 47

Slide 47 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin ● Clone the project on Github IDE support = built-in support = built-in support + plugin

Slide 48

Slide 48 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin ● Clone the project on Github IDE support Community Kotlin Weekly http://www.kotlinweekly.net/ = built-in support = built-in support + plugin

Slide 49

Slide 49 text

Kotlin Koans ● Koans online ● Offline Kotlin Educational Plugin ● Clone the project on Github IDE support Community Kotlin Weekly http://www.kotlinweekly.net/ Kotlin Conf kotlinconf.com + = built-in support plugin = built-in support

Slide 50

Slide 50 text

Thank you! @segunfamisa segunfamisa.com @moyheen medium.com/@moyinoluwa