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

Effective Java In Kotlin World

Effective Java In Kotlin World

Effective Java In Kotlin World presentation slides.

Seyyed davud hosseiny

July 18, 2019
Tweet

More Decks by Seyyed davud hosseiny

Other Decks in Programming

Transcript

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

    y) { println("Same Int"); } else { println("Diff Int”); } Video Link: https://www.youtube.com/playlist?list=PLT2xIm2X7W7jqdoYI5EXpYkuOPkWngOP3
  2. Integer x = 300; Integer y = 300; if(x ==

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

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

    println("Same Int"); } else { println("Diff Int”); } prints Same Int
  5. public class Elvis { private static final Elvis INSTANCE =

    new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { ... } }
  6. object Elvis public final class Elvis { public static final

    Elvis INSTANCE; static { Elvis var0 = new Elvis(); INSTANCE = var0; } }
  7. public class NutritionFacts { private final int servingSize; // required

    private final int servings; // required private final int calories; // optional private final int fat; // optional private final int sodium; // optional private final int carbohydrate; // optional }
  8. public static class Builder { // Required parameters private final

    int servingSize; private final int servings; // Optional parameters - initialized to default values private int calories = 0; private int fat = 0; private int carbohydrate = 0; private int sodium = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder carbohydrate(int val) { carbohydrate = val; return this; } public Builder sodium(int val) { sodium = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } public class NutritionFacts { private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate;
  9. public static class Builder { // Required parameters private final

    int servingSize; private final int servings; // Optional parameters - initialized to default values private int calories = 0; private int fat = 0; private int carbohydrate = 0; private int sodium = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder carbohydrate(int val) { carbohydrate = val; return this; } public Builder sodium(int val) { sodium = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } public class NutritionFacts { private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; private NutritionFacts(Builder builder) { servingSize = builder.servingSize; servings = builder.servings; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbohydrate = builder.carbohydrate; } }
  10. class NutritionFacts 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. class NutritionFacts 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) // default value // default value // default value // default value (
  12. class NutritionFacts 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) // no default value // no default value // default value // default value // default value // default value (
  13. class NutritionFacts @JvmOverloads constructor( 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) // no default value // no default value // default value // default value // default value // default value
  14. val cocaCola = NutritionFacts( servingSize = 240, servings = 8,

    fat = 240, calories = 100, carbohydrates = 27 ) val cocaCola = NutritionFacts( servingSize = 240, servings = 8)
  15. val cocaCola = NutritionFacts( servingSize = 240, servings = 8,

    fat = 240, calories = 100, carbohydrates = 27 ) val cocaCola = NutritionFacts( servingSize = 240, servings = 8)
  16. class EspressoCoffeeMachine : Boiler by DoubleBoiler, Pump by RotaryVane 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; } }
  17. public int getCastValueToInt(String str) { int num = -1; try

    { num = Integer.parseInt(str); } catch (NumberFormatException ignored) {} return num; }
  18. 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 }
  19. 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 }
  20. 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() {
  21. 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; data class Item(val id: String, val name: String, val description: String)
  22. 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; }
  23. 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; }
  24. 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; } }
  25. 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); }
  26. 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 + ")"; }
  27. 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; }
  28. 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") }
  29. 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; } @Parcelize
  30. data class Item(val id: String, val name: String, val description:

    String) @Parcelize 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()); }
  31. data class Item(val id: String, val name: String, val description:

    String) @Parcelize 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;
  32. sealed class RemoteData { object NotInitialized : RemoteData() data class

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

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

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

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

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

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

    is RemoteData.Loading -> { viewModel.loading(request.progress) } is RemoteData.Success -> { viewModel.transform(request.result) } is RemoteData.Failure -> { viewModel.showError(request.exception) } }
  39. public final class Integer extends Number Integer[] integers = new

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

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

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

    = 1L; java.lang.ArrayStoreException: Long cannot be stored in an array of type Integer[]
  43. ?

  44. Stack<Number> stack = new Stack<>(); List<Integer> integers = new ArrayList<>();

    stack.pushAll(integers); class Stack<E> { public void pushAll(Iterable<? extends E> src) { for (E e : src) push(e); } }
  45. 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()); } }
  46. 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()) } }
  47. <out T> T er<in T> { { t: T) interface

    Source fun next(): } interface Consum fun consume( }
  48. + =

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

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

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

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

    Range expressions Operator overloading Companion objects Coroutines Contracts Inline classes