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
30
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
81
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
テストコード書いてみませんか?
onopon
2
270
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
280
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
310
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
210
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
320
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
380
Spatial Rendering for Apple Vision Pro
warrenm
0
300
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
230
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
770
Flatt Security XSS Challenge 解答・解説
flatt_security
0
180
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
990
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
380
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
210
Making Projects Easy
brettharned
116
6k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Side Projects
sachag
452
42k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
190
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
50k
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/