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
33
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
66
Introduction to Functional Programing in Kotlin and Arrow
oshamahue
0
150
Kotlin for Java developers.
oshamahue
0
74
Finding the View
oshamahue
0
110
Mocking Kotlin with MockK
oshamahue
0
82
After I/O Istanbul Jetpack CameraX
oshamahue
0
34
After I/O Kabul Android summary
oshamahue
0
46
Mocking Kotlin with MockK
oshamahue
0
180
RxJava vs Coroutines
oshamahue
1
590
Other Decks in Programming
See All in Programming
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
190
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
13
2.2k
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.8k
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
300
DMMオンラインサロンアプリのSwift化
hayatan
0
170
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
440
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
290
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.2k
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
170
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
335
57k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Site-Speed That Sticks
csswizardry
2
260
Optimising Largest Contentful Paint
csswizardry
33
3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
173
51k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
Into the Great Unknown - MozCon
thekraken
34
1.6k
Automating Front-end Workflow
addyosmani
1366
200k
What's in a price? How to price your products and services
michaelherold
244
12k
A designer walks into a library…
pauljervisheath
205
24k
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/