Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin: Next Level of Android Development

Kotlin: Next Level of Android Development

I gave a talk at Kotlin/Everywhere Lafia, Nassarawa State, Nigeria 19 on Kotlin the next level of Android Development.

Umar Saidu Auna

June 26, 2019
Tweet

More Decks by Umar Saidu Auna

Other Decks in Programming

Transcript

  1. Umar Saidu Auna,
    @umarauna
    otlin: Next level of Android
    Development
    June 26, 2019

    View full-size slide

  2. See what mostly happen to JAVA programmers before
    Kotlin

    View full-size slide

  3. otlin history

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. 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.

    View full-size slide

  7. 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

    View full-size slide

  8. 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.

    View full-size slide

  9. 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).

    View full-size slide

  10. 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

    View full-size slide

  11. 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

    View full-size slide

  12. otlin interesting
    features

    View full-size slide

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

    View full-size slide

  14. #1 Fastest growing language

    View full-size slide

  15. Companies Using Kotlin

    View full-size slide

  16. Companies Using Kotlin

    View full-size slide

  17. Job requirements

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. Null Safety - nullable types and non-nullable types
    Tony Hoare, one of the creators of the programming language ALGOL.
    Protects against NullPointerException the $1,000,000,000 mistake

    View full-size slide

  21. Null Safety - nullable types and non-nullable types
    Tony Hoare, one of the creators of the programming language ALGOL.
    Protects against NullPointerException the $1,000,000,000 mistake
    // compiler error
    var name: String A non-nullable object can’t be null
    name = null

    View full-size slide

  22. Null Safety - nullable types and non-nullable types
    Tony Hoare, one of the creators of the programming language ALGOL.
    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

    View full-size slide

  23. 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

    View full-size slide

  24. 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.

    View full-size slide

  25. 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
    Elvis Presley. His hairstyle resembles a Question Mark

    View full-size slide

  26. 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”

    View full-size slide

  27. 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!!!

    View full-size slide

  28. 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.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. 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

    View full-size slide

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

    View full-size slide

  33. 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()

    View full-size slide

  34. 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

    View full-size slide

  35. 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 ImmutableClassJava(val name: String, val age: Int)
    ● Class is final by default
    ● val implies that the parameters are
    final as well (values can’t be assigned)

    View full-size slide

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

    View full-size slide

  37. 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

    View full-size slide

  38. 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.

    View full-size slide

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

    View full-size slide

  40. 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)
    }

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  44. Iterators
    Java Kotlin

    View full-size slide

  45. When Expression

    View full-size slide

  46. otlin conciseness

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  53. otlin classes

    View full-size slide

  54. 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)

    View full-size slide

  55. 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
    }
    }

    View full-size slide

  56. 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
    }

    View full-size slide

  57. otlin: getting started

    View full-size slide

  58. Important place to learn…….

    View full-size slide

  59. Important place to learn…….
    Tutorials Point - Kotlin

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  63. Important place to learn…….
    Tutorials Point - Kotlin
    Kotlin for Android Developers – Antonio Leiva
    Koans online – try.kotl.in/koans
    Kotlin Bootcamp for Programmers - Udacity
    https://developer.android.com/kotlin/learn

    View full-size slide

  64. Compilations

    View full-size slide

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

    View full-size slide

  66. 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

    View full-size slide

  67. otlin: libraries

    View full-size slide

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

    View full-size slide

  69. 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.

    View full-size slide

  70. Async Server - Client

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  74. 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

    View full-size slide

  75. Thank you!
    @umarauna @umarauna

    View full-size slide