Effective Java to Effective Kotlin

7ace70cd355db1983dea895fbe01a4ef?s=47 rallat
November 20, 2018

Effective Java to Effective Kotlin

7ace70cd355db1983dea895fbe01a4ef?s=128

rallat

November 20, 2018
Tweet

Transcript

  1. None
  2. Integer x = 300; Integer y = 300; if(x ==

    y) { println("Same Int"); } else { println("Diff Int”); }A
  3. Integer x = 300; Integer y = 300; if(x ==

    y) { println("Same Int"); } else { println("Diff Int”); }A prints Diff Int
  4. Integer x = 300; Integer y = 300; if(x.equals(y)) {

    println("Same Int"); } else { println("Diff Int”); }A prints Diff Int
  5. Integer x = 300; Integer y = 300; if(x.equals(y)) {

    println("Same Int"); } else { println("Diff Int”); }A prints Same Int
  6. (ᵒ‒Д‒)ᵒኯᵲᴸᵲ

  7. ᵣᴷᵣ ϊ( ʄ-ʄϊ)

  8. None
  9. None
  10. Effective Java To Effective Kotlin Israel Ferrer Camacho @rallat

  11. None
  12. None
  13. Item 3: Enforce the singleton property with a private constructor

    or an enum type
  14. public enum EffectiveSingleton { INSTANCE }A

  15. object EffectiveSingleton public enum EffectiveSingleton { INSTANCE }A

  16. object EffectiveSingleton public final class EffectiveSingleton { public static final

    EffectiveSingleton INSTANCE; static { EffectiveSingleton var0 = new EffectiveSingleton(); INSTANCE = var0; } }
  17. Item 2: Consider a builder when faced with many constructor

    parameters
  18. public class Item { public final String id; public final

    String name; public final String description; }A
  19. public class Item { public final String id; public final

    String name; public final String description; }A public static class Builder { private String name = ""; private String description = ""; public Item create(String id) { return new Item(id, description, name); }B public Builder name(String name) { this.name = name; return this; }C public Builder description(String description) { this.description = description; return this; }D }E
  20. public static class Builder { private String name = "";

    private String description = ""; public Item create(String id) { return new Item(id, description, name); }B public Builder name(String name) { this.name = name; return this; }C public Builder description(String description) { this.description = description; return this; }D }E
  21. class Item( val id: String, val name: String = "",

    val description: String = "" ) public static class Builder { private String name = ""; private String description = ""; public Item create(String id) { return new Item(id, description, name); }B public Builder name(String name) { this.name = name; return this; }C public Builder description(String description) { this.description = description; return this; }D }E
  22. class Item @JvmOverloads constructor( val id: String, val name: String

    = "", val description: String = "" ) public static class Builder { private String name = ""; private String description = ""; public Item create(String id) { return new Item(id, description, name); }B public Builder name(String name) { this.name = name; return this; }C public Builder description(String description) { this.description = description; return this; }D }E
  23. class Item @JvmOverloads constructor( val id: String, val name: String

    = "", val description: String = "" )
  24. class Item @JvmOverloads constructor( val id: String, val name: String

    = "", val description: String = "" ) Item("1")
  25. class Item @JvmOverloads constructor( val id: String, val name: String

    = "", val description: String = "" ) Item("1") Item(name= "the name",id = "1")
  26. Item 40: Consistently use the Override annotation

  27. interface Header{ public void inflate(); } class AdHeader implements Header

    { public void inflate() { } }
  28. interface Header{ public void inflate(); } class AdHeader implements Header

    { public void inflate() { } }
  29. Item 17: Design for inheritance or else prohibit it

  30. class NewMainActivity

  31. class NewMainActivity

  32. open class NewMainActivity class ClassicActivity: NewMainActivity()

  33. Item 18: Favor composition over inheritance

  34. interface Pump { void startMotor(); } class RotaryVane implements Pump

    interface Boiler { void heat(); } class DoubleBoiler implements Boiler
  35. public class EspressoCoffeeMachine implements Boiler, Pump { private final DoubleBoiler

    boiler; private final RotaryVane pump; public EspressoCoffeeMachine(DoubleBoiler boiler, RotaryVane pump) { this.boiler = boiler; this.pump = pump; } @Override public void heat() { boiler.heat(); } @Override public void startMotor() { pump.startMotor(); } }
  36. class EspressoCoffeeMachine : Boiler by DoubleBoiler, Pump by RotaryVane public

    class EspressoCoffeeMachine implements Boiler, Pump { private final DoubleBoiler boiler; private final RotaryVane pump; public EspressoCoffeeMachine(DoubleBoiler boiler, RotaryVane pump) { this.boiler = boiler; this.pump = pump; } @Override public void heat() { boiler.heat(); } @Override public void startMotor() { pump.startMotor(); } }
  37. class EspressoCoffeeMachine : Boiler by DoubleBoiler, Pump by RotaryVane

  38. public final class EspressoCoffeeMachine implements Boiler, Pump { private final

    DoubleBoiler $$delegate_0; private final RotaryVane $$delegate_1; public EspressoCoffeeMachine() { this.$$delegate_0 = DoubleBoiler.INSTANCE; this.$$delegate_1 = RotaryVane.INSTANCE; } } class EspressoCoffeeMachine : Boiler by DoubleBoiler, Pump by RotaryVane
  39. Item 77: Don't ignore exceptions

  40. public void close(Closeable c) { try { c.close(); } catch

    (IOException ignored) { } }
  41. public int getCastValueToInt(String str) { int num = -1; try

    { num = Integer.parseInt(str); } catch (NumberFormatException ignored) {} return num; }
  42. public int getCastValueToInt(String str) { int num = -1; try

    { num = Integer.parseInt(str); } catch (NumberFormatException ignored) {} return num; } fun getCastValueToInt(string: String) = try { Integer.parseInt(string); } catch (e: NumberFormatException) { -1 }
  43. Item 15 Minimize mutability

  44. Item 8 Obey contract when overriding equals

  45. Item 9: Always override hashCode when you override Equals

  46. public class ItemJava { @NotNull public final String id; @NotNull

    public final String name; @NotNull public final String description; } public ItemJava(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, " this.id = id; this.name = name; this.description = description;
  47. public class ItemJava { @NotNull public final String id; @NotNull

    public final String name; @NotNull public final String description; public ItemJava(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); this.id = id; this.name = name; this.description = description; }A } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof ItemJava) { ItemJava var2 = (ItemJava) var1; return Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) &&
  48. public class ItemJava { @NotNull public final String id; @NotNull

    public final String name; @NotNull public final String description; public ItemJava(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); this.id = id; this.name = name; this.description = description; }A public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof ItemJava) { ItemJava var2 = (ItemJava) var1; return Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description); }B return false; } else { return true; }C }D } public int hashCode() {
  49. public class ItemJava { @NotNull public final String id; @NotNull

    public final String name; @NotNull public final String description; public ItemJava(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); this.id = id; this.name = name; this.description = description; }A public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof ItemJava) { ItemJava var2 = (ItemJava) var1; return Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description); }B return false; } else { return true; }C }D public int hashCode() { return (this.id.hashCode() * 31 + this.name.hashCode()) * 31 + this.description.hashCode(); }E }F
  50. public class ItemJava { @NotNull public final String id; @NotNull

    public final String name; @NotNull public final String description; public ItemJava(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); this.id = id; this.name = name; this.description = description; }A public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof ItemJava) { ItemJava var2 = (ItemJava) var1; return Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description); }B return false; } else { return true; }C }D public int hashCode() { return (this.id.hashCode() * 31 + this.name.hashCode()) * 31 + this.description.hashCode(); }E }F
  51. public class ItemJava { @NotNull public final String id; @NotNull

    public final String name; @NotNull public final String description; public ItemJava(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); this.id = id; this.name = name; this.description = description; }A public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof ItemJava) { ItemJava var2 = (ItemJava) var1; return Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description); }B return false; } else { return true; }C }D public int hashCode() { return (this.id.hashCode() * 31 + this.name.hashCode()) * 31 + this.description.hashCode(); }E }F data class Item(val id: String, val name: String, val description: String)
  52. data class Item(val id: String, val name: String, val description:

    String) public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  53. data class Item(val id: String, val name: String, val description:

    String) public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; }
  54. data class Item(val id: String, val name: String, val description:

    String) public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  55. data class Item(val id: String, val name: String, val description:

    String) @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; }
  56. data class Item(val id: String, val name: String, val description:

    String) public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  57. data class Item(val id: String, val name: String, val description:

    String) public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true; } }
  58. data class Item(val id: String, val name: String, val description:

    String) public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  59. data class Item(val id: String, val name: String, val description:

    String) public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); }
  60. data class Item(val id: String, val name: String, val description:

    String) public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  61. data class Item(val id: String, val name: String, val description:

    String) @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; }
  62. data class Item(val id: String, val name: String, val description:

    String) public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  63. data class Item(val id: String, val name: String, val description:

    String) @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; }
  64. data class Item(val id: String, val name: String, val description:

    String) @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } val(id, name, description) = fooItem() fun fooItem(): Item { return Item("id", "name", "description") }
  65. data class Item(val id: String, val name: String, val description:

    String) : Parcelable public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true; val(id, name, description) = fooItem() fun fooItem(): Item { return Item("id", "name", "description") }
  66. data class Item(val id: String, val name: String, val description:

    String) : Parcelable public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  67. @Parcelize data class Item(val id: String, val name: String, val

    description: String) : Parcelable public final class Item { @NotNull private final String id; @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true;
  68. public void writeToParcel(@NotNull Parcel parcel, int flags) { Intrinsics.checkParameterIsNotNull(parcel, "parcel");

    parcel.writeString(this.id); parcel.writeString(this.name); parcel.writeString(this.description); } @Metadata( mv = {1, 1, 13}, bv = {1, 0, 3}, k = 3 ) public static class Creator implements Parcelable.Creator { @NotNull public final Object[] newArray(int size) { return new Item[size]; } @NotNull public final Object createFromParcel(@NotNull Parcel in) { Intrinsics.checkParameterIsNotNull(in, "in"); return new Item(in.readString(), in.readString(), in.readString()); } } @Parcelize data class Item(val id: String, val name: String, val description: String) : Parcelable
  69. public final class Item { @NotNull private final String id;

    @NotNull private final String name; @NotNull private final String description; @NotNull public final String getId() { return this.id; } @NotNull public final String getName() { return this.name; } @NotNull public final String getDescription() { return this.description; } public Item(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); super(); this.id = id; this.name = name; this.description = description; } @NotNull public final String component1() { return this.id; } @NotNull public final String component2() { return this.name; } @NotNull public final String component3() { return this.description; } @NotNull public final Item copy(@NotNull String id, @NotNull String name, @NotNull String description) { Intrinsics.checkParameterIsNotNull(id, "id"); Intrinsics.checkParameterIsNotNull(name, "name"); Intrinsics.checkParameterIsNotNull(description, "description"); return new Item(id, name, description); } // $FF: synthetic method // $FF: bridge method @NotNull public static Item copy$default(Item var0, String var1, String var2, String var3, int var4, Object var5) { if ((var4 & 1) != 0) { var1 = var0.id; } if ((var4 & 2) != 0) { var2 = var0.name; } if ((var4 & 4) != 0) { var3 = var0.description; } return var0.copy(var1, var2, var3); } public String toString() { return "Item(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ")"; } public int hashCode() { return ((this.id != null ? this.id.hashCode() : 0) * 31 + (this.name != null ? this.name.hashCode() : 0)) * 31 + (this.description != null ? this.description.hashCode() : 0); } public boolean equals(Object var1) { if (this != var1) { if (var1 instanceof Item) { Item var2 = (Item)var1; if (Intrinsics.areEqual(this.id, var2.id) && Intrinsics.areEqual(this.name, var2.name) && Intrinsics.areEqual(this.description, var2.description)) { return true; } } return false; } else { return true; @Parcelize data class Item(val id: String, val name: String, val description: String) : Parcelable
  70. Item 43: Always return an empty collection instead of null

  71. Handling Null references

  72. public List<Item> getItem(String itemId)

  73. @NotNull public List<Item> getItem(@NotNull String itemId)

  74. @NotNull public List<Item> getItem(@NotNull String itemId)

  75. @NotNull public List<Item> getItem(@NotNull String itemId)

  76. @NotNull public List<Item> getItem(@NotNull String itemId) fun getItem(itemId: String) :

    List<Item>
  77. fun getItem(itemId: String) : List<Item>

  78. fun getItem(itemId: String) : List<Item> @NotNull public final List getItem(@NotNull

    String itemId)
  79. fun getItem(itemId: String) : List<Item>

  80. fun getItem(itemId: String) : List<Item>

  81. fun getItem(itemId: String) : List<Item>

  82. fun getItem(itemId: String) : List<Item>

  83. fun getItem(itemId: String) : List<Item>

  84. Chapter 6: Enums and annotations

  85. None
  86. Sealed classes

  87. sealed class RemoteData

  88. sealed class RemoteData { class NotInitialized : RemoteData() }A

  89. sealed class RemoteData { object NotInitialized : RemoteData() }A

  90. sealed class RemoteData { object NotInitialized : RemoteData() class Loading

    : RemoteData() }A
  91. sealed class RemoteData { object NotInitialized : RemoteData() data class

    Loading(val progress: Progress) : RemoteData() }A
  92. sealed class RemoteData { object NotInitialized : RemoteData() data class

    Loading(val progress: Progress) : RemoteData() class Success : RemoteData() }A
  93. sealed class RemoteData { object NotInitialized : RemoteData() data class

    Loading(val progress: Progress) : RemoteData() class Success : RemoteData() class Failure : RemoteData() }A
  94. sealed class RemoteData { object NotInitialized : RemoteData() data class

    Loading(val progress: Progress) : RemoteData() class Success(result: Response) : RemoteData() class Failure : RemoteData() }A
  95. sealed class RemoteData { object NotInitialized : RemoteData() data class

    Loading(val progress: Progress) : RemoteData() class Success(result: Response) : RemoteData() class Failure(exception: Exception) : RemoteData() }A
  96. fun newState(request: RemoteData) = when (request) { RemoteData.NotInitialized -> request

    }A
  97. fun newState(request: RemoteData) = when (request) { RemoteData.NotInitialized -> request

    is RemoteData.Loading -> { viewModel.loading(request.progress) }B }A
  98. fun newState(request: RemoteData) = when (request) { RemoteData.NotInitialized -> request

    is RemoteData.Loading -> { viewModel.loading(request.progress) }B is RemoteData.Success -> { viewModel.transform(request.result) }C }A
  99. fun newState(request: RemoteData) = when (request) { RemoteData.NotInitialized -> request

    is RemoteData.Loading -> { viewModel.loading(request.progress) }B is RemoteData.Success -> { viewModel.transform(request.result) }C is RemoteData.Failure -> { viewModel.showError(request.exception) }D }A
  100. None
  101. Generics

  102. Item 27: Favor generic methods

  103. Reified

  104. inline fun <reified T> Context.start() = startActivity(Intent(this, T::class.java))

  105. inline fun <reified T> Context.start() = startActivity(Intent(this, T::class.java))

  106. inline fun <reified T> Context.start() = startActivity(Intent(this, T::class.java))

  107. inline fun <reified T> Context.start() = startActivity(Intent(this, T::class.java)) start<PurchaseActivity>()

  108. Item 28: Prefer lists to arrays

  109. Java arrays are covariant

  110. public final class Integer extends Number

  111. Integer[] integers = new Integer[1]; Number[] numbers = integers; numbers[0]

    = 1L; public final class Integer extends Number
  112. Integer[] integers = new Integer[1]; Number[] numbers = integers; numbers[0]

    = 1L; java.lang.ArrayStoreException: Long cannot be stored in an array of type Integer[] public final class Integer extends Number
  113. Java List with generics are invariant

  114. None
  115. List enforces type safety at compile time.

  116. Arrays in Kotlin are invariant and allow generics

  117. Integer[] integers = new Integer[1]; Number[] numbers = integers; numbers[0]

    = 1L; java.lang.ArrayStoreException: Long cannot be stored in an array of type Integer[]
  118. Integer[] integers = new Integer[1]; Number[] numbers = integers; numbers[0]

    = 1L; java.lang.ArrayStoreException: Long cannot be stored in an array of type Integer[]
  119. Item 31: Use bounded wildcards to increase API flexibility

  120. class Stack<E> { public void pushAll(Iterable<E> src) { for (E

    e : src) push(e); }A }B
  121. class Stack<E> { public void pushAll(Iterable<E> src) { for (E

    e : src) push(e); }A }B
  122. ?

  123. None
  124. <? extends T>

  125. <? extends T> <? super T>

  126. PECS stands for producer- extends, consumer-super.

  127. class Stack<E> { public void pushAll(Iterable<E> src) { for (E

    e : src) push(e); } }
  128. class Stack<E> { public void pushAll(Iterable<? extends E> src) {

    for (E e : src) push(e); } }
  129. class Stack<E> { public void pushAll(Iterable<? extends E> src) {

    for (E e : src) push(e); }A }B Stack<Number> stack = new Stack<>(); List<Integer> integers = new ArrayList<>(); stack.pushAll(integers);
  130. class Stack<E> { public void pushAll(Iterable<? extends E> src) {

    for (E e : src) push(e); } public void popAll(Collection<? super E> dst) { while (!isEmpty()) dst.add(pop()); } }
  131. Use-site variance

  132. class Stack<E> { public void pushAll(Iterable<? extends E> src) {

    for (E e : src) push(e); } public void popAll(Collection<? super E> dst) { while (!isEmpty()) dst.add(pop()); } }
  133. class Stack<E> { public void pushAll(Iterable<? extends E> src) {

    for (E e : src) push(e); } public void popAll(Collection<? super E> dst) { while (!isEmpty()) dst.add(pop()); } } class Stack<E> { fun pushAll(src: Iterable<out E>) { for (e in src) push(e) } fun popAll(dst: MutableList<in E>) { while (!isEmpty()) dst.add(pop()) } }
  134. Consumer in, Producer out!

  135. Declaration-site variance

  136. interface Source<out T> { fun next(): T } interface Consumer<in

    T> { fun consume(t: T) }
  137. interface Source<out T> { fun next(): T } interface Consumer<in

    T> { fun consume(t: T) }
  138. Generics

  139. Kotlin solves many of the Java language issues

  140. Kotlin brings fluency

  141. itemArray .asSequence() .filter { specialItem.contains(it.id) } .map { it.price }

    .average()
  142. None
  143. Less Boilerplate More Features

  144. None
  145. IntelliJ

  146. IntelliJ Lambda expressions

  147. IntelliJ Lambda expressions Smart casts

  148. IntelliJ Lambda expressions Smart casts String templates

  149. IntelliJ Lambda expressions Smart casts String templates Type inference

  150. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

  151. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

    Range expressions
  152. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

    Range expressions Operator overloading
  153. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

    Range expressions Operator overloading Companion objects
  154. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

    Range expressions Operator overloading Companion objects Coroutines
  155. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

    Range expressions Operator overloading Companion objects Coroutines Contracts
  156. IntelliJ Lambda expressions Smart casts String templates Type inference Typealias

    Range expressions Operator overloading Companion objects Coroutines Contracts Inline classes
  157. Why are we still using Java?

  158. Effective Java To Effective Kotlin Israel Ferrer Camacho @rallat