Share
Embed
Effective Java In Kotlin World
Effective Java In Kotlin World presentation slides.
Seyyed davud hosseiny
July 18, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
142 slides
-
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 -
Integer x = 300; Integer y = 300; if(x ==
y) { println("Same Int"); } else { println("Diff Int”); } prints Diff Int -
Integer x = 300; Integer y = 300; if(x.equals(y) {
println("Same Int"); } else { println("Diff Int”); } prints Diff Int -
Integer x = 300; Integer y = 300; if(x.equals(y) {
println("Same Int"); } else { println("Diff Int”); } prints Same Int -
public class Elvis { private static final Elvis INSTANCE =
new Elvis(); private Elvis() { } public static Elvis getInstance() { return INSTANCE; } public void leaveTheBuilding() { ... } } -
object Elvis public final class Elvis { public static final
Elvis INSTANCE; static { Elvis var0 = new Elvis(); INSTANCE = var0; } } -
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 } -
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; -
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; } } -
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) ( -
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 ( -
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 ( -
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 -
val cocaCola = NutritionFacts( servingSize = 240, servings = 8,
fat = 240, calories = 100, carbohydrates = 27 ) val cocaCola = NutritionFacts( servingSize = 240, servings = 8) -
val cocaCola = NutritionFacts( servingSize = 240, servings = 8,
fat = 240, calories = 100, carbohydrates = 27 ) val cocaCola = NutritionFacts( servingSize = 240, servings = 8) -
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; } } -
public void close(Closeable c) { try { c.close(); } catch
(IOException ignored) { } } -
public int getCastValueToInt(String str) { int num = -1; try
{ num = Integer.parseInt(str); } catch (NumberFormatException ignored) {} return num; } -
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 } -
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 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() { -
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) -
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; } -
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; } -
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; } } -
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); } -
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 + ")"; } -
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; } -
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") } -
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 -
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()); } -
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; -
sealed class RemoteData { object NotInitialized : RemoteData() class Loading
} (val progress: Progress) : RemoteData() -
sealed class RemoteData { object NotInitialized : RemoteData() data class
Loading(val progress: Progress) : RemoteData() class Success : RemoteData() } -
sealed class RemoteData { object NotInitialized : RemoteData() data class
Loading(val progress: Progress) : RemoteData() } class Success: RemoteData() class Failure: RemoteData() -
sealed class RemoteData { object NotInitialized : RemoteData() data class
Loading(val progress: Progress) : RemoteData() } class Success(result: Response): RemoteData() class Failure: RemoteData() -
sealed class RemoteData { object NotInitialized : RemoteData() data class
Loading(val progress: Progress) : RemoteData() } class Success(result: Response): RemoteData() class Failure(exception: Exception): RemoteData() -
fun newState(request: RemoteData) = when (request) { RemoteData.NotInitialized -> request
is RemoteData.Loading -> { viewModel.loading(request.progress) } } -
fun newState(request: RemoteData) = when (request) { RemoteData.NotInitialized -> request
is RemoteData.Loading -> { viewModel.loading(request.progress) } is RemoteData.Success -> { viewModel.transform(request.result) } } -
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) } } -
public final class Integer extends Number Integer[] integers = new
Integer[1]; Number[] numbers = integers; numbers[0] = 1L; -
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[] -
Integer[] integers = new Integer[1]; Number[] numbers = integers; numbers[0]
= 1L; java.lang.ArrayStoreException: Long cannot be stored in an array of type Integer[] -
Integer[] integers = new Integer[1]; Number[] numbers = integers; numbers[0]
= 1L; java.lang.ArrayStoreException: Long cannot be stored in an array of type Integer[] -
class Stack<E> { public void pushAll(Iterable<E> src) { for (E
e : src) push(e); } } -
class Stack<E> { public void pushAll(Iterable<E> src) { for (E
e : src) push(e); } } -
class Stack<E> { public void pushAll(Iterable<E> src) { for (E
e : src) push(e); } } -
class Stack<E> { public void pushAll(Iterable<? extends E> src) {
for (E e : src) push(e); } } -
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); } } -
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> { 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()) } } -
interface Source<out T> { fun next(): T } interface Consumer<in
T> { fun consume(t: T) } -
<out T> T er<in T> { { t: T) interface
Source fun next(): } interface Consum fun consume( } -
IntelliJ Lambda expressions Smart casts String templates Type inference Typealias
Range expressions Operator overloading -
IntelliJ Lambda expressions Smart casts String templates Type inference Typealias
Range expressions Operator overloading Companion objects -
IntelliJ Lambda expressions Smart casts String templates Type inference Typealias
Range expressions Operator overloading Companion objects Coroutines -
IntelliJ Lambda expressions Smart casts String templates Type inference Typealias
Range expressions Operator overloading Companion objects Coroutines Contracts -
IntelliJ Lambda expressions Smart casts String templates Type inference Typealias
Range expressions Operator overloading Companion objects Coroutines Contracts Inline classes