Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How 'Effective Java' influenced Kotlin - Droidcon Vienna 2018

Lukas Lechner
September 22, 2018

How 'Effective Java' influenced Kotlin - Droidcon Vienna 2018

Lukas Lechner

September 22, 2018
Tweet

More Decks by Lukas Lechner

Other Decks in Programming

Transcript

  1. 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 }
  2. 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 }
  3. 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 }
  4. 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 } • • •
  5. • • • • NutritionFacts pie = new NutritionFacts.Builder(240, 8)

    .calories(100) .carbohydrates(10) .protein(4) .build();
  6. 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)
  7. 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)
  8. 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)
  9. 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)
  10. 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)
  11. 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 )
  12. private val isVegan: Boolean = false, private val containsGluten: Boolean

    = false val pie = nutritionFacts(servingSize = 240, servings = 8) { additionalInformation { +isVegan +containsGluten } }
  13. 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!"); } }
  14. 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!"); } }
  15. 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!"); } }
  16. 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!"); } }
  17. 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!"); } }
  18. public class TextUtils { // Suppresses default constructor, ensuring non-instantiability.

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

    private TextUtils() {} ... public static boolean isEmpty(@Nullable CharSequence str) { return str == null || str.length() == 0; } ... }
  20. // File: TextUtils.kt fun isEmpty(CharSequence? str): Boolean { return str

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

    null || this.length == 0 } // Another file fun main(args: Array<String>) { val name: String? = "..." name.isEmpty() }
  22. 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); } }
  23. 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); } }
  24. 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); } }
  25. 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); } }
  26. 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); } }
  27. 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); } }
  28. 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); } }
  29. • • ◦ ◦ ◦ // So we will still

    have performance issues if we use Long? var sum: Long? = 0L
  30. 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; } }
  31. 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 + ")"; }
  32. public class JavaPerson { // don't use public fields in

    public classes! public String name; public Integer age; } • •
  33. 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; } }
  34. class KotlinPerson { var name: String? = null var age:

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

    Int? = null set(value) { if (value in 0..120){ field = value } else{ throw IllegalArgumentException() } } }
  36. + =