Slide 1

Slide 1 text

@lukleDev

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

public class NutritionFacts { private final int servingSize; // required private final int servings; // required private final int calories; optional private final int carbohydrates; // optional private final int protein; // optional private final int fat; // optional }

Slide 6

Slide 6 text

public class NutritionFacts { private final int servingSize; // required private final int servings; // required private final int calories; optional private final int carbohydrates; // optional private final int protein; // optional private final int fat; // optional }

Slide 7

Slide 7 text

public class NutritionFacts { private final int servingSize; // required private final int servings; // required private final int calories; optional private final int carbohydrates; // optional private final int protein; // optional private final int fat; // optional }

Slide 8

Slide 8 text

public class NutritionFacts { private final int servingSize; // required private final int servings; // required private final int calories; optional private final int carbohydrates; // optional private final int protein; // optional private final int fat; // optional } ● ● ●

Slide 9

Slide 9 text

● ● ● ● NutritionFacts pie = new NutritionFacts.Builder(240, 8) .calories(100) .carbohydrates(10) .protein(4) .build();

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

class KotlinNutritionFacts( private val servingSize: Int, private val servings: Int, private val calories: Int = 0, private val fat: Int = 0, private val sodium: Int = 0, private val carbohydrates: Int = 0)

Slide 13

Slide 13 text

class KotlinNutritionFacts( private val servingSize: Int, private val servings: Int, private val calories: Int = 0, private val fat: Int = 0, private val sodium: Int = 0, private val carbohydrates: Int = 0)

Slide 14

Slide 14 text

class KotlinNutritionFacts( private val servingSize: Int, private val servings: Int, private val calories: Int = 0, private val fat: Int = 0, private val sodium: Int = 0, private val carbohydrates: Int = 0)

Slide 15

Slide 15 text

class KotlinNutritionFacts( private val servingSize: Int, private val servings: Int, private val calories: Int = 0, private val fat: Int = 0, private val sodium: Int = 0, private val carbohydrates: Int = 0)

Slide 16

Slide 16 text

class KotlinNutritionFacts( private val servingSize: Int, private val servings: Int, private val calories: Int = 0, private val fat: Int = 0, private val sodium: Int = 0, private val carbohydrates: Int = 0)

Slide 17

Slide 17 text

val pie = KotlinNutritionFacts(240, 8)

Slide 18

Slide 18 text

val pie = KotlinNutritionFacts(240, 8) val pie = KotlinNutritionFacts(240, 8, calories = 100, fat = 2)

Slide 19

Slide 19 text

val pie = KotlinNutritionFacts(240, 8) val pie = KotlinNutritionFacts(240, 8, calories = 100, fat = 2) val pie = KotlinNutritionFacts( servingSize = 240, servings = 8, calories = 100, fat = 2 )

Slide 20

Slide 20 text

● ● ● ● + =

Slide 21

Slide 21 text

private val isVegan: Boolean = false, private val containsGluten: Boolean = false

Slide 22

Slide 22 text

private val isVegan: Boolean = false, private val containsGluten: Boolean = false val pie = nutritionFacts(servingSize = 240, servings = 8) { additionalInformation { +isVegan +containsGluten } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

public class Elvis { private static final Elvis INSTANCE = new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } }

Slide 25

Slide 25 text

public class Elvis { private static final Elvis INSTANCE = new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } }

Slide 26

Slide 26 text

public class Elvis { private static final Elvis INSTANCE = new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } }

Slide 27

Slide 27 text

public class Elvis { private static final Elvis INSTANCE = new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } }

Slide 28

Slide 28 text

public class Elvis { private static final Elvis INSTANCE = new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { System.out.println("Whoa baby, I'm outta here!"); } }

Slide 29

Slide 29 text

object KotlinElvis { fun leaveTheBuilding(){ println("Whoa baby, I'm outta here!") } }

Slide 30

Slide 30 text

● + =

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

public class TextUtils { // Suppresses default constructor, ensuring non-instantiability. private TextUtils() {} ... public static boolean isEmpty(@Nullable CharSequence str) { return str == null || str.length() == 0; } ... }

Slide 33

Slide 33 text

public class TextUtils { // Suppresses default constructor, ensuring non-instantiability. private TextUtils() {} ... public static boolean isEmpty(@Nullable CharSequence str) { return str == null || str.length() == 0; } ... }

Slide 34

Slide 34 text

class TextUtils private constructor(){ ... }

Slide 35

Slide 35 text

// File: TextUtils.kt fun isEmpty(CharSequence? str): Boolean { return str == null || str.length == 0 }

Slide 36

Slide 36 text

// File: TextUtils.kt fun isEmpty(CharSequence? str): Boolean { return str == null || str.length == 0 } // Another file fun main(args: Array) { val name: String? = ".." isEmpty(name) }

Slide 37

Slide 37 text

// File: TextUtils.kt fun String?.isEmpty(): Boolean { return this == null || this.length == 0 }

Slide 38

Slide 38 text

// File: TextUtils.kt fun String?.isEmpty(): Boolean { return this == null || this.length == 0 } // Another file fun main(args: Array) { val name: String? = "..." name.isEmpty() }

Slide 39

Slide 39 text

● ● ● + =

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

public class Sum { // Hideously slow program! public static void main(String[] args) { Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 42

Slide 42 text

public class Sum { // Hideously slow program! public static void main(String[] args) { Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 43

Slide 43 text

public class Sum { // Hideously slow program! public static void main(String[] args) { Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 44

Slide 44 text

public class Sum { // Hideously slow program! public static void main(String[] args) { Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 45

Slide 45 text

public class Sum { // Hideously slow program! public static void main(String[] args) { Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 46

Slide 46 text

public class Sum { // Hideously slow program! public static void main(String[] args) { Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 47

Slide 47 text

public class Sum { // Hideously slow program! public static void main(String[] args) { // primitive long instead of the Long reference type long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } }

Slide 48

Slide 48 text

● ⇔ ● ⇔ ●

Slide 49

Slide 49 text

● ○ ■ ■

Slide 50

Slide 50 text

● ● ●

Slide 51

Slide 51 text

● ● ○ ○ ○ // So we will still have performance issues if we use Long? var sum: Long? = 0L

Slide 52

Slide 52 text

● ● ● ● + =

Slide 53

Slide 53 text

● ● ● ●

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

● equals() ● hashCode() ● toString() ● when how

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

data class Person(val name: String, val age: Int)

Slide 58

Slide 58 text

public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Person) { Person var2 = (Person)var1; if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age) { return true; } } return false; } else { return true; } }

Slide 59

Slide 59 text

public int hashCode() { return (this.name != null ? this.name.hashCode() : 0) * 31 + this.age; }

Slide 60

Slide 60 text

public int hashCode() { return (this.name != null ? this.name.hashCode() : 0) * 31 + this.age; } public String toString() { return "Person(name=" + this.name + ", age=" + this.age + ")"; }

Slide 61

Slide 61 text

● hashCode(), equals() toString() ● ● ● + =

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

public class JavaPerson { // don't use public fields in public classes! public String name; public Integer age; } ● ●

Slide 64

Slide 64 text

public class JavaPerson { private String name; private Integer age; }

Slide 65

Slide 65 text

public class JavaPerson { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

class KotlinPerson { var name: String? = null var age: Int? = null }

Slide 68

Slide 68 text

class KotlinPerson { var name: String? = null var age: Int? = null } kotlinPerson.name

Slide 69

Slide 69 text

class KotlinPerson { var name: String? = null var age: Int? = null } kotlinPerson.name kotlinPerson.age = 18

Slide 70

Slide 70 text

class KotlinPerson { var name: String? = null var age: Int? = null set(value) { if (value in 0..120){ field = value } else{ throw IllegalArgumentException() } } }

Slide 71

Slide 71 text

● ● ● + =

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

● ● final ● ● open

Slide 74

Slide 74 text

● ○ ○ ● ○ ●

Slide 75

Slide 75 text

● ●

Slide 76

Slide 76 text

● ● ● ● ● + =

Slide 77

Slide 77 text

● ● ● ● ● + =

Slide 78

Slide 78 text

+ =