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

Kotlin: what you need to know

Kotlin: what you need to know

I gave a talk on what you need to get started with kotlin in DevFest North West Kano, and how easy its going to be especially to Java Developers. Presented in DevFest North West (Kano) 2018

Umar Saidu Auna

November 17, 2018
Tweet

More Decks by Umar Saidu Auna

Other Decks in Programming

Transcript

  1. Umar Saidu Auna, Flexisaf Edusoft @umarauna otlin: what you need

    to know November 18th, 2018 North West (Kano)
  2. About me North West (Kano) - Android Developer since 2014.

    - Started GDG Minna 2014. - Graduate of BUK CS Student. - Founded Sysbit Nigeria. - Intern @ Flexisaf Edusoft. - Volunteer @ Nigerian Red Cross Society. - and I love meeting new people.
  3. History Statically typed programming language, and runs on the JVM.

    Kotlin was developed by JetBrains (Makers of IntelliJ)
  4. 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.
  5. 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
  6. What is Kotlin Kotlin, as described before, is a JVM

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

    based language developed by JetBrains, 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).
  8. What is Kotlin Kotlin, as described before, is a JVM

    based language developed by JetBrains, 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
  9. What is Kotlin Kotlin, as described before, is a JVM

    based language developed by JetBrains, 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
  10. “In Kotlin, everything is an object (reference type), we don’t

    find primitives types as the ones we can use in Java……...”
  11. Null Safety - nullable types and non-nullable types Protects against

    NullPointerException the $1,000,000,000 mistake
  12. 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
  13. 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
  14. 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
  15. 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.
  16. 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
  17. 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”
  18. 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!!!
  19. 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.
  20. String Interpolation in Java + Kotlin void hello(String name) {

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

    Java? final classes private fields no setters • Immutability in Kotlin? Guess?
  23. 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()
  24. 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
  25. 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<String>) • Class is final by default • val implies that the parameters are final as well (values can’t be assigned)
  26. Functions // function sample fun sampleFunc() { // code goes

    here } A function is declared using the “fun” keyword
  27. 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
  28. 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.
  29. 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) }
  30. Functions - extension functions // extension function fun Int.square(): Int

    { return this * this } fun useExtension() { val square = 2.square() println(square) }
  31. Conciseness in Kotlin public void doSomething() { // do something

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

    } fun doSomething(): Unit { // do same thing }
  33. 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)
  34. 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 } }
  35. 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 }
  36. Important place to learn……. Tutorials Point - Kotlin Kotlin for

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

    Android Developers – Antonio Leiva Koans online – try.kotl.in/koans Kotlin Bootcamp for Programmers - Udacity
  38. 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
  39. 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.