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

Refactoring to Kotlin

Refactoring to Kotlin

A presentation on Kotlin features and how you can convert your existing Java codebase to Kotlin using them.

Jubril

June 22, 2019
Tweet

More Decks by Jubril

Other Decks in Programming

Transcript

  1. Who am I? Jubril Edu Software Engineer @ Cotta &

    Cush Android & iOS @TheJubril Love running and exploring new places
  2. About Kotlin • Unveiled in 2011 • Version 1.0 released

    in 2016 • Open Source • Interoperable with Java • Compiles to JVM bytecode and Javascript • Statically typed
  3. Kotlin features Constructors: // Primary Constructor class Person constructor(firstName: String)

    { ... } // Secondary Constructor class Person { constructor(firstName: String) { ... } }
  4. Kotlin features Collection Types: //immutable interface val articles = setOf("a",

    "A", "an", "An", "the", "The") val numbers = listOf("one", "two", "three", "four") val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1) //mutable interface val set = mutableSetOf(1, 2, 3) val numbers = mutableListOf(1, 2, 3, 4) val numbersMap = mutableMapOf("one" to 1, "two" to 2)
  5. Kotlin features Null Safety: var a: String = "abc" a

    = null // compilation error var b: String? = "abc" b = null // ok print(b) val l = b?.length ?: -1 // Using Elvis Operator val l = b?.length // Using Safe Call val l = b!!.length // Non null Operator
  6. Kotlin features Data Classes: data class User(val name: String, val

    age: Int) Data classes have to fulfill the following requirements: • The primary constructor needs to have at least one parameter; • All primary constructor parameters need to be marked as val or var; • Data classes cannot be abstract, open, sealed or inner;
  7. Kotlin features String Templates: val i = 10 println("i =

    $i") // prints "i = 10" val s = "abc" println("$s.length is ${s.length}") // prints "abc.length is 3" Type Check and Casts: if (obj is String) { print(obj.length) } fun demo(x: Any) { if (x is String) { print(x.length)//x is automatically cast to String } }
  8. Kotlin features Singletons: object DataProviderManager { fun registerDataProvider(provider: DataProvider) {

    // ... } } Companion Objects: class MyClass { companion object Factory { fun create(): MyClass = MyClass()} } val instance = MyClass.create()
  9. Refactoring from Java to Kotlin MVP • Interfaces • Activity

    • Data class • Presenter • Helper Class