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
29
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
62
Introduction to Functional Programing in Kotlin and Arrow
oshamahue
0
150
Kotlin for Java developers.
oshamahue
0
72
Finding the View
oshamahue
0
110
Mocking Kotlin with MockK
oshamahue
0
74
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
580
Other Decks in Programming
See All in Programming
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Jakarta EE meets AI
ivargrimstad
0
580
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
Jakarta EE meets AI
ivargrimstad
0
120
Macとオーディオ再生 2024/11/02
yusukeito
0
370
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Side Projects
sachag
452
42k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Navigating Team Friction
lara
183
14k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Fireside Chat
paigeccino
34
3k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Typedesign – Prime Four
hannesfritz
40
2.4k
Being A Developer After 40
akosma
86
590k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
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/