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

Kotlin Java interop and Kotlin Migration

Hadi Tok
December 07, 2019

Kotlin Java interop and Kotlin Migration

Hadi Tok

December 07, 2019
Tweet

More Decks by Hadi Tok

Other Decks in Programming

Transcript

  1. KOTLIN KONFIGÜRASYONU ➤ Kotlin’i aktive etme ➤ Tools > Kotlin

    > Configure Kotlin in Project ➤ Gradle dependency annotationProcessor -> kapt ➤ Kotlin Interop Lint check ➤ Preferences> Editor> Inspections ➤ Android>Lint > Interoperability>Kotlin Interoperability
  2. GETTER SETTER JAVA private String javaString = "non final string";

    public String getJavaString() { return javaString; } public void setJavaString(String javaString) { this.javaString = javaString; }
  3. JAVA NULL SAFETY private String nullableString = null; public String

    getNullableString() { return nullableString; } public void setNullableString(String nullableString) { this.nullableString = nullableString; }
  4. JAVA TO KOTLIN NULL SAFETY val nullableLength: Int = javaToKotlin.nullableString.length

    val notNullableLength: Int = javaToKotlin.notNullableString.length
  5. JAVA TO KOTLIN NULL SAFETY private String nullableString = null;

    public String getNullableString() { return nullableString; } public void setNullableString(String nullableString) { this.nullableString = nullableString; }
  6. JAVA TO KOTLIN NULL SAFETY @Nullable private String nullableString =

    null; @Nullable public String getNullableString() { return nullableString; } public void setNullableString(@Nullable String nullableString) { this.nullableString = nullableString; }
  7. JAVA TO KOTLIN NULL SAFETY @NotNull private String notNullableString =

    "bar"; @NotNull public String getNotNullableString() { return notNullableString; } public void setNotNullableString(@NotNull String notNullableString) { this.notNullableString = notNullableString; }
  8. JAVA TO KOTLIN NULL SAFETY val nullableLength: Int = javaToKotlin.nullableString.length

    val notNullableLength: Int = javaToKotlin.notNullableString.length
  9. KOTLIN TO JAVA STATIC class KotlinToJava { companion object {

    val companionString = "companion string" fun companionFunction(): String { return "companion function" } } }
  10. KOTLIN TO JAVA STATIC class KotlinToJava { companion object {

    val companionString = "companion string" fun companionFunction(): String { return "companion function" } } }
  11. KOTLIN TO JAVA STATIC class KotlinToJava { companion object {

    @JvmStatic val companionString = "companion string" @JvmStatic fun companionFunction(): String { return "companion function" } } }
  12. HIGHER ORDER FUNCTIONS ➤ Bir fonksiyon döndüren ➤ Parametre olarak

    fonksiyon alabilen ➤ Temelde bir fonksiyona bir değişken olarak davranabilme
  13. HIGHER ORDER FUNCTIONS fun functionReturn(): () -> Unit { val

    func = { println("my function") } //function creation return func }
  14. JAVA TO KOTLIN HIGHER ORDER FUNCTIONS fun functionPass(func: (String) ->

    Unit) { func("myString") //invoking a function }
  15. JAVA TO KOTLIN HIGHER ORDER FUNCTIONS fun functionReturn(): () ->

    Unit { val func = { println("my function") } //function creation return func }
  16. EXTENSIONS ➤ Hali hazırda bulunan Class’lara kalıtım(inheritance) olmadan yeni fonksiyon

    ya da değişken eklemeyi sağlar ➤ Herhangi bir class olabilir ➤ Static olarak derlenir
  17. EXTENSION FUNCTIONS fun String.getThridLetter(): Char { if (this.length > 2)

    { return this.toCharArray()[2] } else throw RuntimeException("Length should be at least 3 ") }