Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin Java interop and Kotlin Migration
Search
Hadi Tok
December 07, 2019
Programming
0
28
Kotlin Java interop and Kotlin Migration
Hadi Tok
December 07, 2019
Tweet
Share
More Decks by Hadi Tok
See All by Hadi Tok
Android Async Programming
oshamahue
0
60
Introduction to Functional Programing in Kotlin and Arrow
oshamahue
0
150
Kotlin for Java developers.
oshamahue
0
66
Finding the View
oshamahue
0
110
Mocking Kotlin with MockK
oshamahue
0
69
After I/O Istanbul Jetpack CameraX
oshamahue
0
34
After I/O Kabul Android summary
oshamahue
0
41
Mocking Kotlin with MockK
oshamahue
0
170
RxJava vs Coroutines
oshamahue
1
570
Other Decks in Programming
See All in Programming
Rechartsで楽にゴリゴリにカスタマイズする!
10tera
1
140
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
200
TypeScriptで 負荷テストを書こう 〜k6のシングルバイナリの秘密〜
dora1998
7
2.9k
Hono・Prisma・AWSでGeoなAPI開発
nokonoko1203
5
640
Using Livebook to build and deploy internal tools @ ElixirConf 2024
hugobarauna
0
230
Rubyとクリエイティブコーディングの輪の広がり / The Growing Circle of Ruby and Creative Coding
chobishiba
1
240
Rustではじめる負荷試験
skanehira
5
1.2k
Jakarta EE meets AI
ivargrimstad
0
130
オートマトン学習しろ / Do automata learning
makenowjust
3
110
Kotlin 2.0 and Beyond
antonarhipov
2
140
全力の跳躍を捉える計測アプリを作る
ogijun2018
1
1.3k
REXML改善のその後
naitoh
0
160
Featured
See All Featured
Done Done
chrislema
180
16k
Adopting Sorbet at Scale
ufuk
72
8.9k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
52k
Debugging Ruby Performance
tmm1
72
12k
[RailsConf 2023] Rails as a piece of cake
palkan
45
4.6k
From Idea to $5000 a Month in 5 Months
shpigford
378
46k
Side Projects
sachag
451
42k
How To Stay Up To Date on Web Technology
chriscoyier
785
250k
Building an army of robots
kneath
302
42k
Designing with Data
zakiwarfel
98
5k
Design by the Numbers
sachag
277
19k
Transcript
KOTLIN'E GEÇIŞ VE JAVA ILE BIRLIKTE KULLANIMI
BEN KIMIM ➤ Lead Android Dev @CizitzenMe ➤ https://haditok.com
KOTLIN ➤ 2011 Yılında Jetbrains tarafından duyuruldu ➤ 2017’de Google
Android Resmi dil olarak duyuruldu
KOTLIN ➤ http://kotlinlang.org/docs/reference/ ➤ https://try.kotlinlang.org/ ➤ https://android.github.io/kotlin-guides/interop.html
SOURCE CODE ➤ https://github.com/oshamahue/KotlinInteropCases
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
JAVA KODUNU KOTLINE ÇEVIRME
GETTER SETTER
GETTER SETTER JAVA private String javaString = "non final string";
public String getJavaString() { return javaString; } public void setJavaString(String javaString) { this.javaString = javaString; }
GETTER SETTER KOTLIN val kotlinValString = "Value"
GETTER SETTER KOTLIN TO JAVA val string = javaToKotlin.javaString javaToKotlin.javaString
= "New variable"
GETTER SETTER JAVA TO KOTLIN String var = kotlinToJava.getKotlinVarString(); kotlinToJava.setKotlinVarString("New
var");
NULL SAFETY
KOTLIN NULL SAFETY var nullableString: String? = null var notNullableString:
String = "foo"
JAVA NULL SAFETY private String nullableString = null; public String
getNullableString() { return nullableString; } public void setNullableString(String nullableString) { this.nullableString = nullableString; }
JAVA TO KOTLIN NULL SAFETY val nullableLength: Int = javaToKotlin.nullableString.length
val notNullableLength: Int = javaToKotlin.notNullableString.length
JAVA TO KOTLIN NULL SAFETY private String nullableString = null;
public String getNullableString() { return nullableString; } public void setNullableString(String nullableString) { this.nullableString = nullableString; }
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; }
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; }
JAVA TO KOTLIN NULL SAFETY val nullableLength: Int = javaToKotlin.nullableString.length
val notNullableLength: Int = javaToKotlin.notNullableString.length
KOTLIN NULLABLE TYPE val nullableLength: Int? = javaToKotlin.nullableString.length val notNullableLength:
Int = javaToKotlin.notNullableString.length
KOTLIN SAFE CALL OPERATOR val nullableLength: Int = javaToKotlin.nullableString ?.length
KOTLIN ELVIS OPERATOR val nullableLength: Int = javaToKotlin.nullableString ?.length ?:
0
KOTLIN !! OPERATOR val nullableLength: Int = javaToKotlin.nullableString ?.length !!
STATIC VS COMPANION OBJECT
JAVA STATIC public static String javaStaticString = "myJavaStaticString";
JAVA STATIC String myString = JavaToKotlin.javaStaticString;
KOTLIN TO JAVA STATIC val myJavaStaticString = JavaToKotlin.javaStaticString
KOTLIN TO JAVA STATIC class KotlinToJava { companion object {
val companionString = "companion string" fun companionFunction(): String { return "companion function" } } }
KOTLIN TO JAVA STATIC val companionValString = KotlinToJava.companionString val companionFunString
= KotlinToJava.companionFunction()
JAVA TO KOTLIN COMPANION OBJECT companionValString = KotlinToJava.Companion.getCompanionString(); companionFunString =
KotlinToJava.Companion.companionFunction();
KOTLIN TO JAVA STATIC class KotlinToJava { companion object {
val companionString = "companion string" fun companionFunction(): String { return "companion function" } } }
KOTLIN TO JAVA STATIC class KotlinToJava { companion object {
@JvmStatic val companionString = "companion string" @JvmStatic fun companionFunction(): String { return "companion function" } } }
JAVA TO KOTLIN COMPANION OBJECT companionValString = KotlinToJava.Companion.getCompanionString(); companionFunString =
KotlinToJava.Companion.companionFunction();
JAVA TO KOTLIN COMPANION OBJECT companionValString = KotlinToJava.getCompanionString(); companionFunString =
KotlinToJava.companionFunction();
PACKAGE LEVEL MEMBERS
PACKAGE LEVEL MEMBERS const val packageLevelConstant = "kotlinToJava" fun packageLevelFunction():
String { return "this is a package level function" }
PACKAGE LEVEL MEMBERS import com.oshamahue.kotlindemo.packageLevelConstant import com.oshamahue.kotlindemo.packageLevelFunction fun otherPackageLevelFunction() {
var string = packageLevelFunction() string = packageLevelConstant }
JAVA TO KOTLIN PACKAGE LEVEL MEMBERS String string = KotlinToJavaKt.packageLevelFunction();
string = KotlinToJavaKt.packageLevelConstant;
JAVA TO KOTLIN PACKAGE LEVEL MEMBERS @file:JvmName("KotlinToJavaUtil") package com.oshamahue.kotlindemo import
java.io.IOException . . . .
JAVA TO KOTLIN PACKAGE LEVEL MEMBERS String string = KotlinToJavaUtil.packageLevelFunction();
string = KotlinToJavaUtil.packageLevelConstant;
HIGHER ORDER FUNCTIONS
HIGHER ORDER FUNCTIONS ➤ Bir fonksiyon döndüren ➤ Parametre olarak
fonksiyon alabilen ➤ Temelde bir fonksiyona bir değişken olarak davranabilme
HIGHER ORDER FUNCTIONS fun functionPass(func: (String) -> Unit) { func("myString")
//invoking a function }
HIGHER ORDER FUNCTIONS functionPass(this ::nullables)
HIGHER ORDER FUNCTIONS val func = { println("my function") }
HIGHER ORDER FUNCTIONS fun functionReturn(): () -> Unit { val
func = { println("my function") } //function creation return func }
JAVA TO KOTLIN HIGHER ORDER FUNCTIONS fun functionPass(func: (String) ->
Unit) { func("myString") //invoking a function }
JAVA TO KOTLIN HIGHER ORDER FUNCTIONS kotlinToJava.functionPass(new Function1<String,Unit>() { @Override
public Unit invoke(String string) { return null; } });
JAVA TO KOTLIN HIGHER ORDER FUNCTIONS fun functionReturn(): () ->
Unit { val func = { println("my function") } //function creation return func }
JAVA TO KOTLIN HIGHER ORDER FUNCTIONS Function0<Unit> func = kotlinToJava.functionReturn();
func.invoke();
EXTENSIONS
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
EXTENSION FUNCTIONS fun String.getThridLetter(): Char { if (this.length > 2)
{ return this.toCharArray()[2] } else throw RuntimeException("Length should be at least 3 ") }
EXTENSION FUNCTIONS val thridLetter = "foo".getThridLetter()
JAVA TO KOTLIN EXTENSION FUNCTIONS char thirdLetter = KotlinToJavaUtil.getThridLetter("foo");
TEŞEKKÜRLER HTTPS://MOCKK.IO/