Save 37% off PRO during our Black Friday Sale! »

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.

5711a187d80c7009bbb5266a7aa8fd67?s=128

Umar Saidu Auna

November 25, 2021
Tweet

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
  2. None
  3. What mostly happen to JAVA programmers before Kotlin

  4. otlin history

  5. History Kotlin is a cross-platform, statically typed, general-purpose programming language

    and runs on the JVM
  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)
  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.
  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
  9. otlin What?

  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.
  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).
  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
  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
  14. otlin interesting features

  15. “In Kotlin, everything is an object (reference type), we don’t

    find primitives types as the ones we can use in Java……...”
  16. #1 Fastest growing language

  17. Companies Using Kotlin

  18. Companies Using Kotlin

  19. Job requirements

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

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

    }
  23. Kotlin 1.3 no more (args: Array<String>) fun main(){ // your

    code goes here }
  24. None
  25. None
  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
  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
  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
  29. None
  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
  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.
  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
  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”
  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!!!
  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.
  36. String Interpolation / template in Kotlin fun hello(name: String) {

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

    System.out.println("Hello, " + name); }
  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
  39. Immutability How does it work in Java? • Immutability in

    Java? final classes private fields no setters • Immutability in Kotlin? Guess?
  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()
  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
  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)
  43. Functions // function sample fun sampleFunc() { // code goes

    here } A function is declared using the “fun” keyword
  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
  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.
  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.
  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.
  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
  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
  50. Functions Others: • Higher order functions • Lambdas • Inline

    functions etc.
  51. None
  52. Iterators Java Kotlin

  53. When Expression

  54. otlin conciseness

  55. Conciseness in Kotlin public void doSomething() { // do something

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

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

    } fun doSomething() { // do same thing }
  58. Conciseness in Kotlin public String getName() { return name; }

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

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

    fun getName() = name
  61. None
  62. otlin class

  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)
  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 } }
  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 }
  66. None
  67. otlin: getting started

  68. Important place to learn…….

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

  70. Important place to learn……. Tutorials Point - Kotlin Kotlin for

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

    Android Developers – Antonio Leiva Koans online – try.kotl.in/koans
  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
  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
  74. Compilations

  75. None
  76. IDE’s

  77. Build Tools

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

  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
  80. otlin: libraries

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

    code etc
  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.
  83. Async Server - Client

  84. Finally 1 You have 3 Options.. Decide Kotlin is not

    For you and continue with Java
  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
  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
  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