Slide 1

Slide 1 text

Kotlin, Start? Start! ֢അࢳ (pluu) Android Developer GDG Korea Android Organizer

Slide 2

Slide 2 text

Agenda ● Kotlin Overview ● Kotlin द੘द ঌݶ જ਷ Ѫ ● Ӓېࢲ??

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Basic fun main(args: Array): Unit { println("Hello, world!") }

Slide 11

Slide 11 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Function Keyword

Slide 12

Slide 12 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Function Name

Slide 13

Slide 13 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Parameter Name

Slide 14

Slide 14 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Parameter Type

Slide 15

Slide 15 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Return Type

Slide 16

Slide 16 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Function Definition

Slide 17

Slide 17 text

Basic fun main(args: Array): Unit { println("Hello, world!") } Unit return type can be omitted

Slide 18

Slide 18 text

Basic fun main(args: Array) { println("Hello, world!") }

Slide 19

Slide 19 text

Null Safe val text: String = null val text: String = null // Error, non-null Type

Slide 20

Slide 20 text

val text: String? = null Null Safe

Slide 21

Slide 21 text

val text: String? = null Nullable Keyword Null Safe

Slide 22

Slide 22 text

val text: String? = null text = 100.toString() val text: String? = null text = 100.toString() // Error, Immutable Data Null Safe

Slide 23

Slide 23 text

val text: String? = null text = 100.toString() Immutable Keyword Null Safe

Slide 24

Slide 24 text

var text: String? = null text = 100.toString() Mutable Keyword Null Safe

Slide 25

Slide 25 text

Null Safe if (data != null && data.value != null) { data.value.toString() }

Slide 26

Slide 26 text

Null Safe data?.value?.toString()

Slide 27

Slide 27 text

Control Flow if / when / try

Slide 28

Slide 28 text

Control Flow statement expression No Return Value Return Value

Slide 29

Slide 29 text

int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; } val a = 10 val value: String if (a > 10) { value = "Over 10" } else { value = "Under 10" } Expression ( if / when / try ) Java Kotlin

Slide 30

Slide 30 text

int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; } val a = 10 val value: String if (a > 10) { value = "Over 10" } else { value = "Under 10" } Expression ( if / when / try ) Java Kotlin

Slide 31

Slide 31 text

Expression ( if / when / try ) Java Kotlin val a = 10 val value = if (a > 10) { "Over 10" } else { "Under 10" } int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; }

Slide 32

Slide 32 text

Expression ( if / when / try ) Java Kotlin val a = 10 val value = if (a > 10) "Over 10" else "Under 10" int a = 10; String value; if (a > 10) { value = "Over 10"; } else { value = "Under 10"; }

Slide 33

Slide 33 text

Expression ( if / when / try ) val a = 10 val value = if (a > 10) "Over 10" else "Under 10" Kotlin

Slide 34

Slide 34 text

Expression ( if / when / try ) Java Kotlin val value: String if (a === 1) { value = "one" } else if (a === 2) { value = "two" } else if (a === 3) { value = "three" } else { value = "another" } String value; if (a == 1) { value = "one"; } else if (a == 2) { value = "two"; } else if (a == 3) { value = "three"; } else { value = "another"; }

Slide 35

Slide 35 text

Expression ( if / when / try ) Java Kotlin val value: String when { a === 1 -> value = "one" a === 2 -> value = "two" a === 3 -> value = "three" else -> value = "another" } String value; if (a == 1) { value = "one"; } else if (a == 2) { value = "two"; } else if (a == 3) { value = "three"; } else { value = "another"; }

Slide 36

Slide 36 text

Expression ( if / when / try ) Java Kotlin val value = when(a) { 1 -> "one" 2 -> "two" 3 -> "three" else -> "another" } String value; if (a == 1) { value = "one"; } else if (a == 2) { value = "two"; } else if (a == 3) { value = "three"; } else { value = "another"; }

Slide 37

Slide 37 text

when (x) { parseInt(s) -> print("s encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Expression ( if / when / try ) Kotlin

Slide 38

Slide 38 text

when (x) { parseInt(s) -> print("s encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Expression ( if / when / try ) Kotlin Expression

Slide 39

Slide 39 text

when (x) { parseInt(s) -> print("s encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Expression ( if / when / try ) Kotlin Range

Slide 40

Slide 40 text

when (x) { parseInt(s) -> print("s encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Expression ( if / when / try ) Kotlin Not In Range

Slide 41

Slide 41 text

when (x) { parseInt(s) -> print("s encodes x") in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Expression ( if / when / try ) Kotlin Optional, statement / expression

Slide 42

Slide 42 text

Default Parameter public String foo(String name, int number, boolean toUpperCase) { return (toUpperCase ? name.toUpperCase() : name) + number; } public String foo(String name, int number) { return foo(name, number, false); } public String foo(String name, boolean toUpperCase) { return foo(name, 42, toUpperCase); } public String foo(String name) { return foo(name, 42); } Java

Slide 43

Slide 43 text

Default Parameter public String foo(String name, int number, boolean toUpperCase) { return (toUpperCase ? name.toUpperCase() : name) + number; } public String foo(String name, int number) { return foo(name, number, false); } public String foo(String name, boolean toUpperCase) { return foo(name, 42, toUpperCase); } public String foo(String name) { return foo(name, 42); } Java Default : false Default : 42

Slide 44

Slide 44 text

Default Parameter fun foo(name: String, number: Int = 42, toUpperCase: Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number

Slide 45

Slide 45 text

Default Parameter fun foo(name: String, number: Int = 42, toUpperCase: Boolean = false) = (if (toUpperCase) name.toUpperCase() else name) + number

Slide 46

Slide 46 text

Example ● setter / getter ● hashCode ● equals ● toString class Item { private String type1; private int type2; private float type3; private float type4; }

Slide 47

Slide 47 text

Example class Item { private String type1; private int type2; private float type3; private float type4; public Item(String type1, int type2, float type3, float type4) { this.type1 = type1; this.type2 = type2; this.type3 = type3; this.type4 = type4; } public String getType1() { return type1;

Slide 48

Slide 48 text

class Item { private String type1; private int type2; private float type3; private float type4; public Item(String type1, int type2, float type3, float type4) { this.type1 = type1; this.type2 = type2; this.type3 = type3; this.type4 = type4; } public String getType1() { return type1; } public void setType1(String type1) { this.type1 = type1; } public int getType2() { return type2; } public void setType2(int type2) { this.type2 = type2; } public float getType3() { return type3; } public void setType3(float type3) { this.type3 = type3; } public float getType4() { return type4; } public void setType4(float type4) { this.type4 = type4; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Item item = (Item) o; if (type2 != item.type2) return false; if (Float.compare(item.type3, type3) != 0) return false; if (Float.compare(item.type4, type4) != 0) return false; return type1 != null ? type1.equals(item.type1) : item.type1 == null; } @Override public int hashCode() { int result = type1 != null ? type1.hashCode() : 0; result = 31 * result + type2; result = 31 * result + (type3 != +0.0f ? Float.floatToIntBits(type3) : 0); result = 31 * result + (type4 != +0.0f ? Float.floatToIntBits(type4) : 0); return result; } @Override public String toString() { return "Item{" + "type1='" + type1 + '\'' + ", type2=" + type2 + ", type3=" + type3 + ", type4=" + type4 + '}'; } } Field ࣻ ߸҃द? ߸҃ ೦ݾ ● setter / getter ● hashCode ● equals ● toString

Slide 49

Slide 49 text

Data Class data class Item( var type1: String?, var type2: Int, var type3: Float, var type4: Float)

Slide 50

Slide 50 text

Data Class data class Item( var type1: String?, var type2: Int, var type3: Float, var type4: Float) data Keyword

Slide 51

Slide 51 text

Data Class ● equals() / hashCode() ● toString() ● componentN() functions ● copy() // Destructuring Declarations val jane = User("Jane", 35) val (name, age) = jane println("$name, $age years of age") // prints "Jane, 35 years of age"

Slide 52

Slide 52 text

Singleton Class public class A { private static final A instance = new A(); private A() {} public static A getInstance() { return instance; } public void b() { System.out.println("Singleton"); } } Java Custom Method

Slide 53

Slide 53 text

Object Class object A { fun b() { println("Singleton") } } // Use A.b()

Slide 54

Slide 54 text

Object Class object A { fun b() { println("Singleton") } } Object declarations

Slide 55

Slide 55 text

Type Inference val s: String = "Value"

Slide 56

Slide 56 text

// String val c = 'V' // Char val i = 10 // Int val l = 10L // Long val f = 10F // Float val d = 10.0 // Double val b = true // Boolean val sb = StringBuilder() // StringBuilder val s = "Value" Type Inference

Slide 57

Slide 57 text

Smart Casts if (obj is String) { print(obj.length) } https://kotlinlang.org/docs/reference/typecasts.html#smart-casts

Slide 58

Slide 58 text

Smart Casts if (obj is String) { print(obj.length) } Type Check

Slide 59

Slide 59 text

Smart Casts if (obj is String) { print(obj.length) } Smart Casts

Slide 60

Slide 60 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) }

Slide 61

Slide 61 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check

Slide 62

Slide 62 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts

Slide 63

Slide 63 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check

Slide 64

Slide 64 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts

Slide 65

Slide 65 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Type Check

Slide 66

Slide 66 text

Smart Casts when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } Smart Casts

Slide 67

Slide 67 text

Extension Function inline fun Fragment.toast(message: CharSequence): Unit = activity.toast(message) fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show() https://github.com/Kotlin/anko/blob/d5a526512b48c5cd2e3b8f6ff14b153c2337aa22/anko/library/static/ commons/src/dialogs/Toasts.kt // Use in Activity/Fragment toast("Hello World")

Slide 68

Slide 68 text

Extension Function fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

Slide 69

Slide 69 text

Extension Function fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show() Receiver Type

Slide 70

Slide 70 text

Extension Function fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show() Function Definition

Slide 71

Slide 71 text

Extension Function fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show() Function Body

Slide 72

Slide 72 text

Higher-Order Functions and Lambdas fun List.map(transform: (T) -> R): List { val result = arrayListOf() for (item in this) result.add(transform(item)) return result }

Slide 73

Slide 73 text

Higher-Order Functions and Lambdas fun List.map(transform: (T) -> R): List { val result = arrayListOf() for (item in this) result.add(transform(item)) return result } Function Type

Slide 74

Slide 74 text

Higher-Order Functions and Lambdas val list = listOf(1, 2, 3) list.map({ x: Int -> x * 2 })

Slide 75

Slide 75 text

Higher-Order Functions and Lambdas val list = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 })

Slide 76

Slide 76 text

Higher-Order Functions and Lambdas val list = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) list.map() { x -> x * 2 }

Slide 77

Slide 77 text

Higher-Order Functions and Lambdas val list = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) list.map() { x -> x * 2 } list.map { x -> x * 2 }

Slide 78

Slide 78 text

Higher-Order Functions and Lambdas val list = listOf(1, 2, 3) list.map({ x: Int -> x * 2 }) list.map({ x -> x * 2 }) list.map() { x -> x * 2 } list.map { x -> x * 2 } list.map { it * 2 }

Slide 79

Slide 79 text

൞ݎಞ ੺ݎಞ

Slide 80

Slide 80 text

੺ݎಞ 1. Kotlin? ● ࢜۽਍ ঱যۄח Ѣࠗх / ۞׬ ழ࠳ / ࠛউх / ৈ۞ ੄ޙٜ … ● Functional Programming ● Hybrid Language о о૑ח ޙઁٜ ● ҡଳ਷ Best Practices ࠗ੤

Slide 81

Slide 81 text

੺ݎಞ 2. ೠӖ … ೠӖ … ೠӖ … ● ݆੉ হযਃ ● ೠӖ۽ ػ ޙࢲ ● ೠӖ۽ ػ ଼

Slide 82

Slide 82 text

੺ݎಞ

Slide 83

Slide 83 text

੺ݎಞ 3. Function Programming ● ೣࣻ ӝ߈ ೐۽Ӓې߁ ӝߨ ● ೣࣻب ё୓ ● FP ҙ۲ ૑ध੉ ೙ਃೣ (ex, Predicate, High-Order Function)

Slide 84

Slide 84 text

੺ݎಞ 3. FP ౱ ݯߡо ୶ୌ೧઼णפ׮. ੺؀ റਗ ইש

Slide 85

Slide 85 text

੺ݎಞ 3. FP ౱ ݯߡо ୶ୌ೧઼णפ׮. ੺؀ റਗ ইש Lambda Clojure High-Order Function Reduce Dispatch Groovy Scala Currying Pure Function Monad Lazy

Slide 86

Slide 86 text

੺ݎಞ 4. Overhead ● Kotlin Compiler ী ੄೧ ࢤࢿغח ௏٘ ● ࢤпೞח Ѫࠁ׮ ؊ ݆਷ ௏٘о ࢤࢿ ms ױਤ ର੉ ೙ਃदী ٜ݅যঠೞח ௏٘ Javaࠁ׮ וܽѪ਷ ಂ౟

Slide 87

Slide 87 text

੺ݎಞ 4. Overhead fun test(x: Any) { if (x is String) { println(x.length) println(x.length) println(x.length) println(x.length) } } public static final void test(@NotNull Object x) { Intrinsics.checkParameterIsNotNull(x, "x"); if (x instanceof String) { int var1 = ((String)x).length(); System.out.println(var1); var1 = ((String)x).length(); System.out.println(var1); var1 = ((String)x).length(); System.out.println(var1); var1 = ((String)x).length(); System.out.println(var1); } } Kotlin Decompile

Slide 88

Slide 88 text

੺ݎಞ 5. kapt ● Kotlin plugin supports annotation processors like Dagger or DBFlow. ● kotlinc ▶︎ Kotlin Stubs ▶︎ javac ▶︎ Final Code ● Clean Build ೧ঠ૑ ઁ؀۽ ز੘ೞӝب ೣ ● ঌ ࣻ হח ী۞ apply plugin: 'kotlin-kapt'

Slide 89

Slide 89 text

੺ݎಞ 6. ੗߄৬ ৮੹ೠ ੉߹਷ ࠛоמ ● Framework Ú Java ● Library Ú Java / Kotlin ● Your Code Ú Java / Kotlin ● Kotlin Compilerо Java Class Codeܳ ݅ٞ ● Java to Kotlin, non-null ё୓ী null ਸ ࠁյ ҃਋ …. ী۞ ߊࢤ

Slide 90

Slide 90 text

൞ݎಞ

Slide 91

Slide 91 text

൞ݎಞ 1. Java ޙઁ ೧Ѿ Java 6 2006 Java 7 2011 Java 8 2014 Android 1.0 2008 Java 7 Support 2013 Java 8 Support Jack / desugar Different for every version Java 9 ????

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

൞ݎಞ 2. Kotlin Android Extensions ● Kotlin Android Extensions ● View Binding, ViewHolder (1.1.4), Parcelable (1.1.4) ● Extension Function ● Lazy ● Anko

Slide 95

Slide 95 text

൞ݎಞ 2. Kotlin Android Extensions import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") } } Kotlin Android Extensions

Slide 96

Slide 96 text

൞ݎಞ 2. Kotlin Android Extensions apply plugin: 'kotlin-android-extensions' import kotlinx.android.synthetic.main..* // kotlinx.android.synthetic.main.activity_main.*

Slide 97

Slide 97 text

൞ݎಞ 3. טযաח Kotlin GitHub ഛੋ (2017/09/20 ӝળ) ● Stars : 5ਤ ● Language : 30ਤ

Slide 98

Slide 98 text

൞ݎಞ 3. טযաח Kotlin ● ࠶۽Ӓ / ߊ಴ / ҳӖ ࢠ೒ী Kotlin ࢎਊ੉ ૐо ● ӝઓ Java 100% ▶ Java (N %) + Kotlin (M %) ● ׮೯൤, ೠӖ۽ ੘ࢿغח Ӗٜ੉ ࢤӝҊ ੓਺ ● ٘٣য, ೠӖ۽ ػ ଼ 1ӂ੉ ୹द ● ழפ੄ ௏ౣܽ (୹द ৘੿)

Slide 99

Slide 99 text

൞ݎಞ 4. Simple is Best ● Delegation ● Data Class ● Singleton ● Null Safety ● Expression ● Destructuring Declarations ● Type Checks and Casts

Slide 100

Slide 100 text

val person = Person().apply { name = "Pluu" age = 1 money = 1 phone = "123-1234-1234" // More } ൞ݎಞ 5. Code Scope

Slide 101

Slide 101 text

val person = Person().apply { name = "Pluu" age = 1 money = 1 phone = "123-1234-1234" // More } ൞ݎಞ 5. Code Scope apply Calls the specified function [block] with `this` value as its receiver and returns `this` value.

Slide 102

Slide 102 text

val person = Person().apply { … } ൞ݎಞ 5. Code Scope

Slide 103

Slide 103 text

൞ݎಞ 6. IntelliJ ● Show Kotlin Bytecode ▶ Decompile ● Decompile ػ Java ௏٘۽ ୓௼ оמ

Slide 104

Slide 104 text

Preferred / Used Kotlin

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

Any.kt

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

http://tech.lezhin.com/2017/08/03/the-case-against-kotlin

Slide 111

Slide 111 text

● 2017֙ ୡࠗఠ Kotlin ҕࠗ ౥౥൤ (पઁ ҕࠗ 3~4ѐਘ) ● GDG Kotlin झఠ٣ ଵৈ ● Kotlin ੗ܐ (Kotlin Reference, Twitter, Medium, Qiita) ● ࢎղ Kotlin झఠ٣ ૓೯ (ঌ۰઱Ҋ ղо ॶ۰Ҋ) ղо ೠ ೯ز

Slide 112

Slide 112 text

૑Ә द੘೧ب ן૑ঋইਃ ● ੉ઁ ݆਷ ࢎۈٜীѱ ҙबਸ ߉ӝ द੘ ● ই૒ ੗ܐо ੸૑݅, Բળೞѱ Ӗٜ੉ ৢۄ১ ● ੗߄৬ э੉ ࢎਊ೧ب غח ੼ ● ௏٘о ब೒ೞѱ ੘ࢿ оמ೧૗ ● best practice ࠗ੤ ● ಩և਷ ٣੗ੋ ಁఢ ੸ਊ оמ

Slide 113

Slide 113 text

● ן૑ ঋওযਃ ● Kotlin ೙ࣻ X, খਵ۽ Kotlin਷ ழ૕ ৘੿ ● Convert Java File to Kotlin File ۽ оߺѱ द੘ ● Show Kotlin Bytecode ۽ ઺р ୓௼ оמ ର੉ܳ ݅٘ח Action Item!

Slide 114

Slide 114 text

ଵҊ ● Kotlin Reference (https://kotlinlang.org/docs/reference/) ● Get Started with Kotlin — It is here to stay (https://medium.com/peachstudio/get- started-with-kotlin-it-is-here-to-stay-1eaac85ae6a0) ● Kotlin: A New Hope in a Java 6 Wasteland (https://academy.realm.io/posts/ droidcon-michael-pardo-kotlin/) ● Kotlin੄ ࡄҗ Ӓܿ੗ (http://tech.lezhin.com/2017/08/03/the-case-against-kotlin)

Slide 115

Slide 115 text

Q&A GDG Slack #android #kotlin ଻օ (Slack оੑ ݂௼ : http://slack.gdg.kr/)