$30 off During Our Annual Pro Sale. View Details »

Kotlin: What you need to know

Kotlin: What you need to know

Kotlin is a cross-platform, statically typed, general-purpose programming 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.

Umar Saidu Auna

November 25, 2021
Tweet

More Decks by Umar Saidu Auna

Other Decks in Programming

Transcript

  1. This work is licensed under the Apache 2 license.
    This work is licensed under the Apache 2 license.
    Android Development with Kotlin v1.0 1
    Kotlin: What you
    need to know
    1
    1
    Umar Saidu Auna
    Software Engineer, gidimo
    tech community Organizer
    umarauna
    November 24, 2021

    View Slide

  2. View Slide

  3. What mostly happen to JAVA programmers before Kotlin

    View Slide

  4. otlin history

    View Slide

  5. History
    Kotlin is a cross-platform, statically typed, general-purpose
    programming language and runs on the JVM

    View Slide

  6. History
    Kotlin is a cross-platform, statically typed, general-purpose
    programming language and runs on the JVM.
    Kotlin was developed by JetBrains (Makers of IntelliJ)

    View Slide

  7. History
    Kotlin is a cross-platform, statically typed, general-purpose 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 Slide

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

  9. otlin What?

    View Slide

  10. What is Kotlin
    Kotlin is a cross-platform, statically typed, general-purpose programming
    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 Slide

  11. What is Kotlin
    Kotlin is a cross-platform, statically typed, general-purpose programming
    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 Slide

  12. What is Kotlin
    Kotlin is a cross-platform, statically typed, general-purpose programming
    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 Slide

  13. What is Kotlin
    Kotlin is a cross-platform, statically typed, general-purpose programming
    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 Slide

  14. otlin interesting
    features

    View Slide

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

    View Slide

  16. #1 Fastest growing language

    View Slide

  17. Companies Using Kotlin

    View Slide

  18. Companies Using Kotlin

    View Slide

  19. Job requirements

    View Slide

  20. View Slide

  21. http://bit.ly/kotlin-image-carousel
    implemeantation 'com.github.UmarAuna:Carousels-Kotlin:0.0.2'

    View Slide

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

    View Slide

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

    View Slide

  24. View Slide

  25. View Slide

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

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

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

  29. View Slide

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

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

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

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

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

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

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

    View Slide

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

    View Slide

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

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

    View Slide

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

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

    View Slide

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

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

    View Slide

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

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

  46. Functions - infix functions
    // infix function
    infix fun Int.times(x: Int): Int {
    return this * x
    }
    We can use the function as an arithmetic operator, i.e.,
    using it without writing dots and parentheses.

    View Slide

  47. 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)
    }
    We can use the function as an arithmetic operator, i.e.,
    using it without writing dots and parentheses.

    View Slide

  48. Functions - extension functions
    // extension function
    fun Int.square(): Int {
    return this * this
    }
    Extension function is a member function of a class that is
    defined outside the class

    View Slide

  49. Functions - extension functions
    // extension function
    fun Int.square(): Int {
    return this * this
    }
    fun useExtension() {
    val square = 2.square()
    println(square)
    }
    Extension function is a member function of a class that is
    defined outside the class
    // usage

    View Slide

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

    View Slide

  51. View Slide

  52. Iterators
    Java Kotlin

    View Slide

  53. When Expression

    View Slide

  54. otlin conciseness

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  61. View Slide

  62. otlin class

    View Slide

  63. Class in Java vs Data Class 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 Slide

  64. Class in Java vs Regular Class 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 Slide

  65. Class in Java vs Regular Class 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 Slide

  66. View Slide

  67. otlin: getting started

    View Slide

  68. Important place to learn…….

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

  74. Compilations

    View Slide

  75. View Slide

  76. IDE’s

    View Slide

  77. Build Tools

    View Slide

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

    View Slide

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

  80. otlin: libraries

    View Slide

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

    View Slide

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

  83. Async Server - Client

    View Slide

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

    View Slide

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

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

  87. This work is licensed under the Apache 2 license.
    This work is licensed under the Apache 2 license.
    Android Development with Kotlin v1.0 87
    Questions?
    Thank you!
    87
    87
    Umar Saidu Auna
    Software Engineer, gidimo
    tech community Organizer
    umarauna

    View Slide