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
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
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Jakarta EE meets AI
ivargrimstad
0
220
Better Code Design in PHP
afilina
PRO
0
130
Jakarta EE meets AI
ivargrimstad
0
130
ヤプリ新卒SREの オンボーディング
masaki12
0
130
subpath importsで始めるモック生活
10tera
0
310
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Featured
See All Featured
What's new in Ruby 2.0
geeforr
343
31k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Thoughts on Productivity
jonyablonski
67
4.3k
Side Projects
sachag
452
42k
Statistics for Hackers
jakevdp
796
220k
The Language of Interfaces
destraynor
154
24k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
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/