$30 off During Our Annual Pro Sale. View Details »

Introduction to Kotlin

Introduction to Kotlin

Presentation slides of our presentation at Kotlin Tel Aviv User Group meetup

Alexander Gherschon

June 19, 2017
Tweet

More Decks by Alexander Gherschon

Other Decks in Programming

Transcript

  1. View Slide

  2. Hello :)

    View Slide

  3. Together.
    Learn.

    View Slide

  4. Agenda
    • Quick history / what is Kotlin / why Kotlin / For whom
    • Variables
    • Functions & extension functions
    • Classes & Properties
    • Lambdas & Collections

    View Slide

  5. What is Kotlin
    • Programming Language developed by Jetbrains (IntelliJ)
    • Open sourced in 2011, Kotlin 1.0 on Feb 15th 2016
    • 100% interoperable with Java
    • On I/O 2017 became first class citizen language on Android
    • Consice, Safe, Interoperable,

    View Slide

  6. Concise?
    public class JavaPerson {
    private String firstName;
    private String lastName;
    public JavaPerson(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    @Override
    public String toString() {
    return "Person{" +
    "firstName='" + firstName + '\'' +
    ", lastName='" + lastName + '\'' +
    '}';
    }
    @Override
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    JavaPerson person = (JavaPerson) o;
    if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) return
    false;
    return lastName != null ? lastName.equals(person.lastName) : person.lastName == null;
    }
    @Override
    public int hashCode() {
    int result = firstName != null ? firstName.hashCode() : 0;
    result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
    return result;
    }
    }

    View Slide

  7. Concise!
    data class Person(var firstName: String,
    var lastName: String)

    View Slide

  8. Why Kotlin?
    • Costs nothing to adopt
    • Concise language
    • No runtime overhead
    • Solves the Nullability problem
    • Lots of modern features

    View Slide

  9. For whom?
    • Android developers (JVM)
    • Backend developers (JVM)
    • iOS developers (LLVM)
    • Windows developers (LLVM)
    • Javascript developers

    View Slide

  10. Introduction to Kotlin

    View Slide

  11. Packages and Files
    • Can be same as Java
    • Functions do not need to be in a class to work
    • All code shown may be under the same file

    View Slide

  12. Packages and Files

    View Slide

  13. Variables

    View Slide

  14. Val (read-only)
    val firstName: String = "Rhaquel"
    val firstName = "Rhaquel"
    firstName = “Alex” // cannot be reassigned

    View Slide

  15. Var (mutable)
    var lastName: String = "Valencia"
    var lastName = “Valencia"
    lastName = "Gherschon"
    lastName = 5 // Int not type of String

    View Slide

  16. Object Instantiation
    sb.append(" is some").append(" text!”)
    println(sb)
    val sb: StringBuilder = StringBuilder(“Here") // no ‘new’ keyword

    View Slide

  17. Functions / Extension Functions

    View Slide

  18. Functions
    fun maximum(first: Int, second: Int): Int {
    if (first > second) {
    return first
    } else {
    return second
    }
    }

    View Slide

  19. Functions - ‘if’ is an expression
    fun maximum(first: Int, second: Int): Int {
    return if (first > second) {
    first
    } else {
    second
    }
    }

    View Slide

  20. Functions - Default values
    fun maximum(first: Int = 3, second: Int = 4): Int {
    return if (first > second) {
    first
    } else {
    second
    }
    }

    View Slide

  21. Calling Functions
    var max = maximum(1, 2) // Regular call
    max = maximum(first = 1, second = 2)// Named Parameters
    max = maximum(second = 2, first = 1)
    max = maximum(second = 8)

    View Slide

  22. Functions - concise?
    fun maximum(first: Int, second: Int): Int {
    return if (first > second) {
    first
    } else {
    second
    }
    }

    View Slide

  23. Functions - concise!
    fun maximum(first: Int, second: Int): Int =
    if (first > second) {
    first
    } else {
    second
    }

    View Slide

  24. Functions - concise!!
    fun maximum(first: Int, second: Int) =
    if (first > second) {
    first
    } else {
    second
    }

    View Slide

  25. Functions - concise!!!
    fun maximum(first: Int, second: Int) =
    if (first > second) first else second
    fun maximum(first: Int, second: Int) = if (first > second) first else second

    View Slide

  26. Functions - When
    fun testResult(value: Any?) = when {
    value == 3 -> "value is exactly 3"
    value is Int -> "double the value = ${value * 2}"
    value == "What the fudge?" -> "Swich case + if statement!"
    else -> "No value"
    }
    val value: Any? = 3
    println(testResult(value))

    View Slide

  27. Functions - When - Concise
    fun testResult(value: Any?) = when (value) {
    3 -> "value is exactly 3"
    is Int -> "double the value = ${value * 2}"
    "What the fudge?" -> "Swich case + if statement!"
    else -> "No value"
    }
    val value: Any? = 3
    println(testResult(value))

    View Slide

  28. Extension functions
    fun Int.maximum(other: Int) =
    if (this > other) this else other
    max = 3.maximum(4)

    View Slide

  29. Extension functions - infix
    infix fun Int.maximum(other: Int) =
    if (this > other) this else other
    max = 3 maximum 4

    View Slide

  30. Null Safety

    View Slide

  31. A look at the nullability problem
    String aString = "Not Null";
    // Some time passes
    aString = null;
    // Some more time passes
    aString.equals(“some value”);

    View Slide

  32. Enter the Nullable Type
    val a: String = “cannot be null"
    val b: String? = null

    View Slide

  33. Unwrapping Nullables
    b?.equals(“Other string”) // Safe
    b!!.equals(“Other String”) // Unsafe

    View Slide

  34. Nullable Chaining
    b?.toDoubleOrNull()?.compareTo(1)
    if (b != null) {
    if (Double.valueOf(b) != null) {
    return Double.valueOf(b).compareTo(1.0);
    }
    }
    return null;

    View Slide

  35. Elvis Operator
    val result = b?.toDoubleOrNull()?.equals(1) ?: false

    View Slide

  36. Classes

    View Slide

  37. Classes
    class Person(val firstName: String,
    var lastName: String)

    View Slide

  38. Classes
    class Person constructor(val firstName: String,
    var lastName: String)

    View Slide

  39. Classes
    class Person constructor(fName: String, lName: String) {
    val firstName: String = fName
    var lastName = lName
    }

    View Slide

  40. Classes public final class Person {
    @NotNull
    private final String firstName;
    @NotNull
    private String lastName;
    @NotNull
    public final String getFirstName() {
    return this.firstName;
    }
    @NotNull
    public final String getLastName() {
    return this.lastName;
    }
    public final void setLastName(@NotNull String var1) {
    Intrinsics.checkParameterIsNotNull(var1, "");
    this.lastName = var1;
    }
    public Person(@NotNull String fName, @NotNull String lName) {
    Intrinsics.checkParameterIsNotNull(fName, "fName");
    Intrinsics.checkParameterIsNotNull(lName, "lName");
    super();
    this.firstName = fName;
    this.lastName = lName;
    }
    }

    View Slide

  41. Properties
    class Person constructor(private val firstName: String,
    private var lastName: String) {
    fun getFullName() = "$firstName $lastName”
    }

    View Slide

  42. Properties
    public final class Person {
    private final String firstName;
    private String lastName;
    @NotNull
    public final String getFullName() {
    return "" + this.firstName + ' ' + this.lastName;
    }
    public Person(@NotNull String firstName, @NotNull String lastName) {
    Intrinsics.checkParameterIsNotNull(firstName, "firstName");
    Intrinsics.checkParameterIsNotNull(lastName, "lastName");
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    }
    }

    View Slide

  43. Properties
    class Person(private var firstName: String,
    private var lastName: String) {
    var fullName
    get() = "$firstName $lastName"
    set(value) {
    val split = value.split(" ")
    firstName = split[0]
    lastName = split[1]
    }
    }

    View Slide

  44. Properties
    public final class Person {
    private String firstName;
    private String lastName;
    @NotNull public final String getFullName() {
    return "" + this.firstName + ' ' + this.lastName;
    }
    public final void setFullName(@NotNull String value) {
    List split = StringsKt.split$default(…);
    this.firstName = (String)split.get(0);
    this.lastName = (String)split.get(1);
    }
    public Person(@NotNull String firstName, @NotNull String lastName) {
    (…)
    }
    }

    View Slide

  45. Lambdas

    View Slide

  46. Lambdas
    val sum3: { x: Int, y: Int -> x + y }
    val sum: (x: Int, y: Int) -> Int = { x, y -> x + y }
    val sum2: (Int, Int) -> Int = { x, y -> x + y }

    View Slide

  47. Lambdas
    val sum: (x: Int, y: Int) -> Int = { x, y -> x + y }
    /** A function that takes 2 arguments. */
    public interface Function2 : Function {
    /** Invokes the function with the specified arguments. */
    public operator fun invoke(p1: P1, p2: P2): R
    }

    View Slide

  48. Collections
    val result = list.map { it + 2 }
    .filter { value -> value % 2 == 0 }
    .firstOrNull{ it > 5 }
    val list = arrayOf(1, 2, 3, 4, 5)

    View Slide

  49. Sequences
    val result = list.asSequence()
    .map { it + 2 }
    .filter { value -> value % 2 == 0 }
    val list = arrayOf(1, 2, 3, 4, 5)
    .firstOrNull{ it > 5 }

    View Slide

  50. Questions?
    Almost done :)

    View Slide

  51. Resources
    • https://www.meetup.com/KotlinTLV/
    • https://www.facebook.com/groups/KotlinTLV
    • https://github.com/KotlinTLV
    • http://slack.kotlinlang.org/
    • https://try.kotlinlang.org/
    • http://kotlinlang.org/docs/reference/
    • https://kotlin.link/

    View Slide

  52. Thank you!

    View Slide