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

Introduction to Kotlin

Introduction to Kotlin

Some basic stuff to show what Kotlin can do better in comparison to Java

Avatar for Hamid Moazzami

Hamid Moazzami

June 06, 2018

Other Decks in Programming

Transcript

  1. Kotlin type system Variables var name = "Salvador" name =

    21 The integer literal does not conform to the expected type String
  2. Kotlin type system Variables - Immutability val name = "Salvador"

    name = "Sanchez" Val cannot be reassigned
  3. Kotlin Functions Default parameters fun boilSome(shalgham: Shalgham, duration: Int) {G

    shalgham.boil(duration)G }G G boilSome(Shalgham(), 1000)
  4. Kotlin Functions Default parameters fun boilSome(shalgham: Shalgham, duration: Int =

    1000) {G shalgham.boil(duration) }G G boilSome(Shalgham(), 1000)G
  5. Kotlin Functions Default parameters fun boilSome(shalgham: Shalgham, duration: Int =

    1000) {G shalgham.boil(duration) }G G boilSome(Shalgham())G
  6. Kotlin Functions Default parameters public class ShalghamView extends View {

    public ShalghamView(Context context) { super(context, null, 0); }G public ShalghamView(Context context, @Nullable AttributeSet attrs) { super(context, attrs, 0); }G public ShalghamView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }G }G
  7. Kotlin Functions Default parameters public class ShalghamView extends View {

    public ShalghamView(Context context) { super(context, null, 0); }G public ShalghamView(Context context, @Nullable AttributeSet attrs) { super(context, attrs, 0); }G public ShalghamView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }G }G class ShalghamView : View { constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr) }
  8. Kotlin Functions Lambdas view.setOnClickListener { it.visibility = View.GONE }) view.setOnClickListener(new

    View.OnClickListener() { @Override public void onClick(View view) { view.setVisibility(View.GONE); } });
  9. Kotlin Functions Extension Functions class StringUtils { public static String

    capitalize(String input) { return Character.toUpperCase(input.charAt(0)) + input.substring(1); }G }G
  10. Kotlin Functions Extension Functions class StringUtils { public static String

    capitalize(String input) { return Character.toUpperCase(input.charAt(0)) + input.substring(1); }G }G String capped = StringUtils.capitalize("someString");
  11. Kotlin Classes Declaration class Person { val name: String val

    age: Int constructor(name: String, age: Int) { this.name = name this.age = age } }
  12. Kotlin Classes Declaration class Person(val name: String, val age: Int)

    { fun fly() { print("People can't fly, can they?") }G }G
  13. Kotlin Classes Declaration class Person(val name: String, val age: Int)

    { private fun fly() { print("People can't fly, can they?") }G }G
  14. Kotlin Classes Properties class Person { var name: String val

    age: Int constructor(name: String, age: Int) { this.name = name this.age = age }G }G val person = Person("Salvador", 26) person.name = "Sanchez"
  15. Kotlin Classes Properties class Person { var name: String get()

    { // Return something } set(value) { // Set the value } val age: Int constructor(name: String, age: Int) { this.name = name this.age = age }G }G val person = Person("Salvador", 26) person.name = "Sanchez"
  16. Kotlin Classes Data class class Person { private String name;

    private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
  17. Kotlin Null Safety Optionals var name = "Salvador" name =

    null Null cannot be a value of non-null type String
  18. Kotlin Null Safety Optionals We crunched anonymized stats from over

    a 1,000 applications monitored by OverOps’s error analysis micro-agent
  19. Kotlin Null Safety Optionals We crunched anonymized stats from over

    a 1,000 applications monitored by OverOps’s error analysis micro-agent 70% of the production environments that we looked at, NPEs were the most occurred exceptions.
  20. Kotlin Null Safety Optionals Tony Hoare “I call it my

    billion-dollar mistake. It was the invention of the null reference in 1965.
 This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.”
  21. Kotlin Null Safety Optionals var name = "Salvador" name =

    null Null cannot be a value of non-null type String
  22. Kotlin Null Safety Optionals val name: String? = null if

    (name != null) { print(name.length) }
  23. Kotlin Null Safety Smart Cast class Person { public int

    age = 26; public static void main(String[] args) { Object person = new Person(); if (person instanceof Person) System.out.print(((Person) person).age); }G }G
  24. Kotlin Null Safety Smart Cast class Person { public int

    age = 26; public static void main(String[] args) { Object person = new Person(); if (person instanceof Person) System.out.print(((Person) person).age); }G }G
  25. Kotlin Null Safety Smart Cast class Person { public int

    age = 26; public static void main(String[] args) { Object person = new Person(); if (person instanceof Person) System.out.print(((Person) person).age); }G }G
  26. Kotlin Null Safety Smart Cast class Person { public int

    age = 26; public static void main(String[] args) { Object person = new Person(); if (person instanceof Person) System.out.print(((Person) person).age); }G }G
  27. Kotlin Null Safety Smart Cast class Person { public int

    age = 26; public static void main(String[] args) { Object person = new Person(); if (person instanceof Person) System.out.print(((Person) person).age); }G }G fun main(args: Array<String>) { val person: Any = Person() if (person is Person) print(person.age) }G
  28. Kotlin Null Safety Smart Cast class Person { public int

    age = 26; public static void main(String[] args) { Object person = new Person(); if (person instanceof Person) System.out.print(((Person) person).age); }G }G fun main(args: Array<String>) { val person: Any = Person() if (person is Person) print(person.age) }G
  29. Kotlin Null Safety Optionals val name: String? = null if

    (name != null) { print(name.length) }
  30. Kotlin Null Safety Optionals @Nullable private String getAvatarUrl() { if

    (getUser() != null && getUser().getProfile() != null && getUser().getProfile().getAvatar() != null) { return getUser().getProfile().getAvatar().getUrl(); }G return null; }G
  31. Kotlin Null Safety Optionals @Nullable private String getAvatarUrl() { if

    (getUser() != null && getUser().getProfile() != null && getUser().getProfile().getAvatar() != null) { return getUser().getProfile().getAvatar().getUrl(); }G return null; }G fun getAvatarUrl(): String? { return getUser()?.profile?.avatar?.url }
  32. Kotlin Coroutines Coroutines loadToken("resource1") { result -> //Perform some operations

    with result networkRequest(result) { result2 -> //Some more things to be done on result2 saveToDatabase(result2) { result3 -> //And a lot more for result3 } } }
  33. Kotlin Coroutines Coroutines val result = async { loadToken("resource1") }.await()

    // Perform some operations with result val result2 = async { networkRequest(result) }.await() // Some more things to be done with result2 val result3 = async { saveToDatabase(result2)} // And at last operations with result3
  34. Kotlin Coroutines Coroutines suspend fun load(token: String) { val result

    = loadToken(token) // Perform some operations with result val result2 = networkRequest(result) // Some more things to be done with result2 val result3 = saveToDatabase(result2) // And at last operations with result3 }
  35. Kotlin and Java Interoperability Kotlin is 100% interoperable with Java!

    • Kotlin code can be called directly from Java and vice versa • Kotlin code can live along side Java • Projects can start using Kotlin without any changes to previous
 Java code and/or convert gradually •IDE makes the conversion process easier