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 Types and compatibility with Java
Search
きりみん
November 21, 2017
Programming
2
2k
Kotlin Types and compatibility with Java
きりみん
November 21, 2017
Tweet
Share
More Decks by きりみん
See All by きりみん
AndroidエンジニアがRailsにチャレンジしてる理由
kirimin
1
1.5k
What are AtCoder and competitive programming
kirimin
0
9.9k
バーチャル男声幼女プログラマーとして活動した1年間の振り返り
kirimin
0
1k
アプリエンジニアでも神絵師になりたい!
kirimin
4
5.4k
Watashi ni Kotlin ga maiorita
kirimin
0
500
NEMのAPIとモザイクであそぼう
kirimin
0
370
はじめようきれいなコード
kirimin
8
3k
Material Components for Android触ってみる
kirimin
7
1.9k
[社内LT]あたらしいMaterial Design
kirimin
1
1.7k
Other Decks in Programming
See All in Programming
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
300
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
950
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
2025.01.17_Sansan × DMM.swift
riofujimon
2
560
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
Swiftコンパイラ超入門+async関数の仕組み
shiz
0
180
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
940
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.4k
return文におけるstd::moveについて
onihusube
1
1.4k
ErdMap: Thinking about a map for Rails applications
makicamel
1
660
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
67
4.5k
Git: the NoSQL Database
bkeepers
PRO
427
64k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Fireside Chat
paigeccino
34
3.1k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Cult of Friendly URLs
andyhume
78
6.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
98
18k
We Have a Design System, Now What?
morganepeng
51
7.3k
Transcript
SwiftΤϯδχΞʹհ͢Δ KotlinͷܕͱJavaͱͷޓੑ Swift/Kotlin߹ಉษڧձ #2
ࣗݾհ
ΘΓ͔ΜΛݱۚͰਫ਼ࢉ͠ͳͯ͘ޙ͔ΒεϚϗ্Ͱٻͨ͠Γग़དྷΔ ΞϓϦͩΑʂΘΓ͔Μ(ۉׂ)͡Όͳͯ͑͘ΔΑʂϥϯνͱ͔ʂ Android൛΄΅KotlinͰ࣮͞Ε͍ͯΔΑʂ
Kotlinʹ͍͓ͭͯ͞Β͍
Swiftͱಉ͘͡Nullڐ༰ܕͱ ඇڐ༰ܕ͕͋Δ • ѻ͍ํ͍͍ͩͨಉ͡ var str1: String = “hallo” str1
= null // ίϯύΠϧΤϥʔ var str2: String? = “konichiwa” str2 = null str2.length // ίϯύΠϧΤϥʔ str2?.length
Swiftͱಉ͘͡جຊతʹ ΠϛϡʔλϒϧͳΛ༻͢Δ • ѻ͍ํ͍͍ͩͨಉ͡ var str1 = “konichiwa” str1 =
“hallo” val str2 = “konichiwa” str2 = “hallo” // ίϯύΠϧΤϥʔ
ϓϦϛςΟϒܕͱࢀরܕ (ΦϒδΣΫτ)Λ۠ผ͠ͳ͍ • JavaʹϓϦϛςΟϒܕͱΦϒδΣΫτͱͯ͠ѻ ͏ͨΊͷϥούʔΫϥε͕͕͋ͬͨɺKotlinͰ۠ ผ͠ͳ͍ • int(Integer) → Int
• long(Long) → Long • boolean(Boolean) → Boolean
ίϨΫγϣϯෆมͱՄมʹ ͔Ε͍ͯΔ • σϑΥϧτͷίϨΫγϣϯෆม • ૢ࡞͍ͨ͠߹MutableListͳͲΛ͏ val list = listOf(“a”,
“b”, “c”) list.add(“d”) // ίϯύΠϧΤϥʔʂ(add()remove()ϝιου͑ͳ͍) val mList = mutableListOf(“a”, “b”, “c”) mList.add(“d”)
KotlinͷίϨΫγϣϯ֓ཁ $PMMFDUJPO .VUBCMF$PMMFDUJPO -JTU .VUBCMF-JTU
ͱ͜ΖͰɺKotlinJavaͱ શʹޓੑ͕͋Γ·͢
None
Java͔ΒݟͨNullඇڐ༰ܕ • KotlinͷNullඇڐ༰ܕJavaͰ@NotNullΞ ϊςʔγϣϯʹͳΔ fun getStr(str: String): String { return
str } ↓ @NotNull public String getStr(@NotNull String str) { Intrinsics.checkParameterIsNotNull(str, "str"); return str; }
Kotlin͔ΒݟͨJavaม • JavaͰఆٛ͞ΕͨΛKotlin͔Βࢀর͢Δͱϓ ϥοτϑΥʔϜܕͱ͍͏ಛघͳܕʹͳΔ public String getStr(String str) { return
str; } ↓ fun getStr(str: String!): String! { return str }
ϓϥοτϑΥʔϜܕʁ • String!ͷΑ͏ʹදݱ͞ΕΔܕ • Nullڐ༰ܕ͔ඇڐ༰ܕ͔ෆ໌ͳঢ়ଶ • Ξϯϥοϓ͠ͳͯͦ͘ͷ··ࢀরͰ͖Δ • ࣮ͨͩ͠ߦ࣌ʹNullPointerException͕ൃੜ͠͏Δ •
JavaଆͰ@Nullable@NotNull͕͍͍ͯΕਖ਼͍͠ܕʹม͞ΕΔ • ͪͳΈʹintͳͲͷϓϦϛςΟϒܕnullʹͳΓ͑ͳ͍ͷͰৗʹඇڐ༰ ܕͱͯ͠ѻ͑Δ
ͳͥϓϥοτϑΥʔϜܕʁ • ͱͱJavaͰఆٛ͞Εͨͯ͢Nullableܕ ͱͯ͠ѻ͏Ҋ͋ͬͨ • ͔ͦ͠͠ΕͰJavaͷϥΠϒϥϦΛࢀরͨ͠߹ͳ Ͳʹίʔυ͕?ͩΒ͚ʹͳͬͯ͠·͍ݱ࣮తͰͳ͍ • ݱ࣮తͳརศੑΛॏࢹ͠ɺϓϥοτϑΥʔϜܕΛɹ ఆٛͨ͠
Java͔ΒݟͨKotlinίϨΫγϣϯ • KotlinͷListMutableListJava͔Β java.util.Listʹݟ͑Δ fun getList(list: List<String>): List<String> { return
list } ↓ @NotNull public List getList(@NotNull List list) { Intrinsics.checkParameterIsNotNull(list, "list"); return list; }
Java͔ΒݟͨKotlinίϨΫγϣϯ • JavaͷίϨΫγϣϯͱKotlinͷίϨΫγϣϯ ޓੑ͕͋Γಛʹҙࣝ͢Δ͜ͱͳ͘ѻ͏ࣄ͕ Ͱ͖Δ • ͨͩ͠KotlinͰෆมͰએݴ͞Ε͍ͯΔίϨΫ γϣϯͰJava͔ΒมߋͰ͖ͯ͠·͏
Kotlin͔ΒݟͨJavaίϨΫγϣϯ • JavaͷListKotlin͔ΒMutableͳListͱͯ͠ ͦͷ··ѻ͑Δ • ListΛฦ͢JavaͷϝιουΛoverride͢Δ߹ ͳͲMutableͳListͱͯ͠Immutableͳ Listͱͯ͠ఆٛग़དྷΔ
KotlinͷίϨΫγϣϯ֓ཁ $PMMFDUJPO .VUBCMF$PMMFDUJPO -JTU .VUBCMF-JTU "SSBZ-JTU
Java͔ΒݟͨIntܕ • جຊతʹintʹม͞ΕΔ • ͨͩ͠ɺInt?ܕArray<Int>ܕ(ྻ)ͳͲΦ ϒδΣΫτͱͯ͠ѻ͏ඞཁ͕͋ΔͨΊɺ Integerܕͱͯ͠ѻΘΕΔ
Kotlin͔ΒݟͨϦςϥϧ • ٯʹKotlin͔ΒݟͨJavaͷϦςϥϧIntͳͲ ͷKotlinͷΦϒδΣΫτͱͯ͠ѻΘΕΔ
·ͱΊ • KotlinJavaͷΫϥεͱҧײͳ͘ڞଘͰ͖Δ ࣄΛڧ͘ҙࣝͨ͠ݴޠ༷ʹͳ͍ͬͯΔ • ݱ࣮తͳ։ൃݱͰͷརศੑΛॏࢹͨ͠ࣄ͕ Kotlin͕ਓؾݴޠʹͳͬͨཧ༝ͷҰ͔ͭ͠Ε ͳ͍
Kotlinͷ༷ΛͬͱΓͨ͘ͳͬ ͨΒʮKotlin in Actionʯ͕Φεεϝ ࠓճͨ͠Α͏ͳ༰͕ͱ͔ͯΓ͘͢ղ આ͞Ε͍ͯ·͢ʂ