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

An Introduction to Kotlin

An Introduction to Kotlin

Kotlin sure has been receiving a lot of buzz lately, is there something to it? Developed by JetBrains, Kotlin has been catching on lately because of its null safety guarantees, functional nature, type inference, full interoperability with Java, cross-platform support, and ease of use with Android and Spring. Is this something you and your team should considering spending time to learn?

One of the most interesting aspects of Kotlin is its design philosophy. Bugs that exist in Java such as NullPointerExceptions are not possible in Kotlin. This mentality of preventing common bugs at the language level exists all through the Kotlin syntax and standard library. Kotlin is also far less verbose than Java, reducing the amount of code you need to read in order to understand it. It also runs on JVMs down to Java 6, without sacrificing features. So if you are in a constrained environment such as Android or an enterprise shop struggling to upgrade, Kotlin might be worth considering.

In this talk, you will learn where Kotlin came from, what its major features are, and why people are using it more and more. At the end of the talk, you will be in a position to make an educated decision about whether Kotlin is right for you!

Todd Ginsberg

April 16, 2020
Tweet

More Decks by Todd Ginsberg

Other Decks in Technology

Transcript

  1. @ToddGinsberg
    An Introduction
    To Kotlin
    Utah Java Users Group
    2020-04-16
    Todd Ginsberg
    Principal Software Developer

    View Slide

  2. @ToddGinsberg
    What’s in a Name?
    By Editors of Open Street Map - Open Street Map, CC

    View Slide

  3. @ToddGinsberg
    What’s in a Name?
    Kotlin Island
    By Editors of Open Street Map - Open Street Map, CC

    View Slide

  4. @ToddGinsberg
    Agenda
    - What Is Kotlin?

    View Slide

  5. @ToddGinsberg
    Agenda
    - What Is Kotlin?
    - Am I Alone?

    View Slide

  6. @ToddGinsberg
    Agenda
    - What Is Kotlin?
    - Am I Alone?
    - Syntax & Features

    View Slide

  7. @ToddGinsberg
    Agenda
    - What Is Kotlin?
    - Am I Alone?
    - Syntax & Features
    - Summary

    View Slide

  8. @ToddGinsberg
    Agenda
    - What Is Kotlin?
    - Am I Alone?
    - Syntax & Features
    - Summary
    - Questions and Possibly Answers

    View Slide

  9. @ToddGinsberg
    Who is this Guy?
    Todd Ginsberg

    View Slide

  10. @ToddGinsberg
    Who is this Guy?
    Todd Ginsberg
    Just moved to Raleigh from Chicago

    View Slide

  11. @ToddGinsberg
    Who is this Guy?
    Todd Ginsberg
    Just moved to Raleigh from Chicago
    Principal Developer at Netspend
    - A payments company in Austin, TX!
    - We love Java and Kotlin!

    View Slide

  12. @ToddGinsberg
    One More Thing...
    I

    View Slide

  13. @ToddGinsberg
    What Is Kotlin?

    View Slide

  14. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains

    View Slide

  15. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains
    Released under Apache 2.0 license

    View Slide

  16. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains
    Released under Apache 2.0 license
    Designed as a general purpose language

    View Slide

  17. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains
    Released under Apache 2.0 license
    Designed as a general purpose language
    ● Targets JVM 6 or 8 bytecode

    View Slide

  18. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains
    Released under Apache 2.0 license
    Designed as a general purpose language
    ● Targets JVM 6 or 8 bytecode
    ● Targets ECMAScript 5.1

    View Slide

  19. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains
    Released under Apache 2.0 license
    Designed as a general purpose language
    ● Targets JVM 6 or 8 bytecode
    ● Targets ECMAScript 5.1
    ● Targets other native platforms thanks to LLVM

    View Slide

  20. @ToddGinsberg
    Major Features
    100% Interoperable with Java

    View Slide

  21. @ToddGinsberg
    Major Features
    100% Interoperable with Java
    Designed to avoid entire classes of defects

    View Slide

  22. @ToddGinsberg
    Major Features
    100% Interoperable with Java
    Designed to avoid entire classes of defects
    Lots of small improvements that add up

    View Slide

  23. @ToddGinsberg
    Major Features
    100% Interoperable with Java
    Designed to avoid entire classes of defects
    Lots of small improvements that add up
    Far less code to accomplish the same task in Java
    - Less cognitive load on developers

    View Slide

  24. @ToddGinsberg
    Major Features
    Null-safe

    View Slide

  25. @ToddGinsberg
    Major Features
    Null-safe
    199,000+ issues on GitHub!

    View Slide

  26. @ToddGinsberg
    Major Features
    Null-safe
    199,000+ issues on GitHub!
    15,650 duplicates on Stack Overflow!

    View Slide

  27. @ToddGinsberg
    Major Features
    Pragmatic Improvements
    No shame in copying language features that
    make developers more productive

    View Slide

  28. @ToddGinsberg
    Major Features
    Positive Community

    View Slide

  29. @ToddGinsberg
    Major Features
    Positive Community
    Even on Stack Overflow!

    View Slide

  30. @ToddGinsberg
    Major Features
    Positive Community
    Even on Stack Overflow!
    RTFM!

    View Slide

  31. @ToddGinsberg
    Major Features
    Positive Community
    Even on Stack Overflow!
    RTFM!

    View Slide

  32. @ToddGinsberg
    Am I Alone?

    View Slide

  33. @ToddGinsberg
    Supported on Android
    2017
    It should work,
    give it a try!

    View Slide

  34. @ToddGinsberg
    Supported on Android
    2018
    Prefer Kotlin
    over Java

    View Slide

  35. @ToddGinsberg
    Supported on Android
    2019
    Android is now
    Kotlin first!

    View Slide

  36. @ToddGinsberg
    Spring Framework Support
    Kotlin is fully supported since Spring Framework 5

    View Slide

  37. @ToddGinsberg
    Spring Framework Support
    Kotlin is fully supported since Spring Framework 5
    Kotlin is an option on start.spring.io

    View Slide

  38. @ToddGinsberg
    Spring Framework Support
    Kotlin is fully supported since Spring Framework 5
    Kotlin is an option on start.spring.io
    Spring @NotNull annotations == Better Kotlin nullability support

    View Slide

  39. @ToddGinsberg
    Spring Framework Support
    Kotlin is fully supported since Spring Framework 5
    Kotlin is an option on start.spring.io
    Spring @NotNull annotations == Better Kotlin nullability support
    Comprehensive Kotlin documentation and examples

    View Slide

  40. @ToddGinsberg
    Gradle DSL

    View Slide

  41. @ToddGinsberg
    Syntax

    View Slide

  42. @ToddGinsberg
    Variables and Values
    var place: String = "Salt Lake City"

    View Slide

  43. @ToddGinsberg
    Variables and Values
    var place: String = "Salt Lake City"
    place = "UT" // OK!

    View Slide

  44. @ToddGinsberg
    Variables and Values
    var place: String = "Salt Lake City"
    place = "UT" // OK!
    val name: String = "Todd"

    View Slide

  45. @ToddGinsberg
    Variables and Values
    var place: String = "Salt Lake City"
    place = "UT" // OK!
    val name: String = "Todd"
    name = "Emma" // Compile Error!

    View Slide

  46. @ToddGinsberg
    Type Inference
    val d: Int = 2
    val text: String = "Todd has $d doughnuts"

    View Slide

  47. @ToddGinsberg
    Type Inference
    val d: Int = 2
    val text: String = "Todd has $d doughnuts"

    View Slide

  48. @ToddGinsberg
    Type Inference
    val d = 2
    val text = "Todd has $d doughnuts"

    View Slide

  49. @ToddGinsberg
    Equality
    val name1 = "EXAMPLE"
    val name2 = "example"

    View Slide

  50. @ToddGinsberg
    Equality
    val name1 = "EXAMPLE"
    val name2 = "example"
    // Structural Equality
    name1 == name2.toUpperCase() // True!

    View Slide

  51. @ToddGinsberg
    Equality
    val name1 = "EXAMPLE"
    val name2 = "example"
    // Structural Equality
    name1 == name2.toUpperCase() // True!
    // Referential Equality
    name1 === name2 // False!

    View Slide

  52. @ToddGinsberg
    Raw Strings
    val json = "{\n\"name\": \"Todd\"\n}"

    View Slide

  53. @ToddGinsberg
    Raw Strings
    val json = "{\n\"name\": \"Todd\"\n}"
    val json = """{ "name": "Todd" }"""

    View Slide

  54. @ToddGinsberg
    Raw Strings
    val json = "{\n\"name\": \"Todd\"\n}"
    val json =
    """
    {
    "name": "Todd"
    }
    """

    View Slide

  55. @ToddGinsberg
    Null Safety
    // Guaranteed to never be null
    var name: String = "Todd"

    View Slide

  56. @ToddGinsberg
    Null Safety
    // Guaranteed to never be null
    var name: String = "Todd"
    // May be null
    var salary: Int? = null

    View Slide

  57. @ToddGinsberg
    Null-Safe Traversal
    var city: String? = "Salt Lake City"

    View Slide

  58. @ToddGinsberg
    Null-Safe Traversal
    var city: String? = "Salt Lake City"
    // Not allowed, might be null!
    city.toUpperCase()

    View Slide

  59. @ToddGinsberg
    Null-Safe Traversal
    var city: String? = "Salt Lake City"
    // Not allowed, might be null!
    city.toUpperCase()
    // Safe traversal
    city?.toUpperCase()

    View Slide

  60. @ToddGinsberg
    Elvis
    val lowest : Int? = listOf(1, 2, 3).min()

    View Slide

  61. @ToddGinsberg
    Elvis
    val lowest : Int? = listOf(1, 2, 3).min()
    val lowest : Int = listOf(1, 2, 3).min() ?: 0

    View Slide

  62. @ToddGinsberg
    Combine Safe-Traversal and Elvis
    println(
    city?.toUpperCase() ?: ”UNKNOWN”
    )

    View Slide

  63. @ToddGinsberg
    Manual Override
    val lowest: Int? = listOf(1, 2, 3).min()

    View Slide

  64. @ToddGinsberg
    Manual Override
    val lowest: Int? = listOf(1, 2, 3).min()
    val lowest: Int = listOf(1, 2, 3).min()!!

    View Slide

  65. @ToddGinsberg
    Manual Override
    val lowest: Int? = listOf(1, 2, 3).min()
    val lowest: Int = listOf(1, 2, 3).min()!!
    val lowest: Int = emptyList().min()!!

    View Slide

  66. @ToddGinsberg
    Manual Override
    val lowest: Int? = listOf(1, 2, 3).min()
    val lowest: Int = listOf(1, 2, 3).min()!!
    val lowest: Int = emptyList().min()!!
    // NullPointerException!

    View Slide

  67. @ToddGinsberg
    Null Safety
    Remember the names!

    View Slide

  68. @ToddGinsberg
    Null Safety
    Remember the names!
    ?. == Safe Traversal

    View Slide

  69. @ToddGinsberg
    Null Safety
    Remember the names!
    ?. == Safe Traversal
    ?: == Elvis

    View Slide

  70. @ToddGinsberg
    Null Safety
    Remember the names!
    ?. == Safe Traversal
    ?: == Elvis
    !! == Hold My Beer

    View Slide

  71. @ToddGinsberg
    Expressions - if
    val status = if (code == 42) {
    "Success"
    } else {
    "Fail"
    }

    View Slide

  72. @ToddGinsberg
    Expressions - try/catch
    val number = try {
    code.toInt()
    } catch (e: NumberFormatException) {
    0
    }

    View Slide

  73. @ToddGinsberg
    Expressions - when
    val result = when (x) {
    0 -> "x is 0”
    in 1..10 -> "x is between 1 and 10"
    in someSet -> "x is in someSet"
    is SomeType -> "x is an instance of SomeType"
    parseString(s) -> "the same as parseString"
    else -> "x doesn't match anything"
    }

    View Slide

  74. @ToddGinsberg
    Smart Casting
    when (x) {
    is Int -> print(x % 2 == 0)
    is String -> print(x.length + 1)
    is IntArray -> print(x.sum())
    }

    View Slide

  75. @ToddGinsberg
    Smart Casting
    when (x) {
    is Int -> print(x % 2 == 0)
    is String -> print(x.length + 1)
    is IntArray -> print(x.sum())
    }

    View Slide

  76. @ToddGinsberg
    Smart Casting
    if(x != null) {
    println(x.toString())
    }

    View Slide

  77. @ToddGinsberg
    Classes
    class Entity : SomeInterface {
    // ...
    }

    View Slide

  78. @ToddGinsberg
    Classes - Inheritance
    open class Entity : SomeInterface {
    // ...
    }
    class Customer : Entity() {
    // ...
    }

    View Slide

  79. @ToddGinsberg
    Properties
    public class Customer {
    private String name;
    public String getName() {
    return name;
    }
    public void setName(final String name) {
    this.name = name;
    }
    }

    View Slide

  80. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    }

    View Slide

  81. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    }
    val c = Customer()

    View Slide

  82. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    }
    val c = Customer()
    c.name = "Todd"

    View Slide

  83. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    }
    val c = Customer()
    c.name = "Todd"
    println("My name is ${c.name}")

    View Slide

  84. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    }

    View Slide

  85. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    set(value) {
    field = value?.toUpperCase()
    }
    }

    View Slide

  86. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    private set(value) {
    field = value?.toUpperCase()
    }
    }

    View Slide

  87. @ToddGinsberg
    Properties
    class Customer {
    var name: String? = null
    get() {
    return field?.toUpperCase()
    }
    }

    View Slide

  88. @ToddGinsberg
    Property Delegation
    class Country {
    val gdp: Int = slowCalculation()
    }

    View Slide

  89. @ToddGinsberg
    Property Delegation
    class Country {
    val gdp: Int by lazy {
    slowCalculation()
    }
    }

    View Slide

  90. @ToddGinsberg
    Class Delegation
    // Java
    class MyJavaImpl implements HugeInterface {
    private final HugeInterface backing;
    public MyJavaImpl(HugeInterface hi) {
    backing = hi;
    }
    // IMPLEMENT EVERYTHING
    }

    View Slide

  91. @ToddGinsberg
    Class Delegation
    // Java
    class MyJavaImpl extends SomeImplementation {
    // Implement Some
    }

    View Slide

  92. @ToddGinsberg
    Class Delegation
    // Kotlin
    class MyImpl(h: HugeInterface) : HugeInterface by h {
    // Implement Some
    }

    View Slide

  93. @ToddGinsberg
    Sealed Classes
    sealed class Message
    class StartServer(val name: String) : Message()
    class StopServer(val name: String) : Message()
    object CountServers : Message()

    View Slide

  94. @ToddGinsberg
    Sealed Classes
    val response = when(msg) {
    is StartServer -> startServer(msg.name)
    is StopServer -> stopServer(msg.name)
    is CountServers -> countServers()
    }

    View Slide

  95. @ToddGinsberg
    Let’s Write a POJO!
    public class Person {
    public String firstName;
    public String lastName;
    }

    View Slide

  96. @ToddGinsberg
    Let’s Write a POJO!
    public class Person {
    private String firstName;
    private String lastName;
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    }

    View Slide

  97. @ToddGinsberg
    Let’s Write a POJO!
    public class Person {
    private String firstName;
    private String lastName;
    public Person() { }
    public Person(final String firstName, final String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    }

    View Slide

  98. @ToddGinsberg
    Let’s Write a POJO!
    import java.util.Objects;
    public class Person {
    private String firstName;
    private String lastName;
    public Person() { }
    public Person(final String firstName, final String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    @Override
    public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final Person person = (Person) o;
    return Objects.equals(firstName, person.firstName) &&
    Objects.equals(lastName, person.lastName);
    }
    @Override
    public int hashCode() {
    return Objects.hash(firstName, lastName);
    }
    }

    View Slide

  99. @ToddGinsberg
    Let’s Write a POJO!
    import java.util.Objects;
    public class Person {
    private String firstName;
    private String lastName;
    public Person() { }
    public Person(final String firstName, final String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    @Override
    public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final Person person = (Person) o;
    return Objects.equals(firstName, person.firstName) &&
    Objects.equals(lastName, person.lastName);
    }
    @Override
    public int hashCode() {
    return Objects.hash(firstName, lastName);
    }
    @Override
    public String toString() {
    return "Person{" +
    "firstName='" + firstName + '\'' +
    ", lastName='" + lastName + '\'' +
    '}';
    }
    }

    View Slide

  100. @ToddGinsberg
    Data Classes to the Rescue!
    data class Person(val firstName: String,
    val lastName: String)

    View Slide

  101. @ToddGinsberg
    Data Classes to the Rescue!
    data class Person(val firstName: String,
    val lastName: String)
    ● Getters (and Setters for vars) as Properties

    View Slide

  102. @ToddGinsberg
    Data Classes to the Rescue!
    data class Person(val firstName: String,
    val lastName: String)
    ● Getters (and Setters for vars) as Properties
    ● toString()

    View Slide

  103. @ToddGinsberg
    Data Classes to the Rescue!
    data class Person(val firstName: String,
    val lastName: String)
    ● Getters (and Setters for vars) as Properties
    ● toString()
    ● hashCode() and equals()

    View Slide

  104. @ToddGinsberg
    Data Classes to the Rescue!
    data class Person(val firstName: String,
    val lastName: String)
    ● Getters (and Setters for vars) as Properties
    ● toString()
    ● hashCode() and equals()
    ● And…

    View Slide

  105. @ToddGinsberg
    Copying Data Classes
    val me = Person("Todd", "Ginsberg")

    View Slide

  106. @ToddGinsberg
    Copying Data Classes
    val me = Person("Todd", "Ginsberg")
    val emma = me.copy(firstName = "Emma")
    // Person(”Emma", "Ginsberg")

    View Slide

  107. @ToddGinsberg
    Destructuring Data Classes
    val me = Person("Todd", "Ginsberg")
    val (first, last) = me
    // first == “Todd” last == “Ginsberg”

    View Slide

  108. @ToddGinsberg
    FUNctions!
    fun generateRandomNumber(): Int {
    return 4
    }

    View Slide

  109. @ToddGinsberg
    FUNctions!
    fun generateRandomNumber(): Int {
    return 4
    }
    fun generateRandomNumber(): Int = 4

    View Slide

  110. @ToddGinsberg
    FUNctions!
    fun generateRandomNumber(): Int {
    return 4
    }
    fun generateRandomNumber(): Int = 4
    fun generateRandomNumber() = 4

    View Slide

  111. @ToddGinsberg
    FUNctions - Default Values
    fun random(offset: Int = 0): Int =
    offset + 4

    View Slide

  112. @ToddGinsberg
    FUNctions - Default Values
    fun random(offset: Int = 0): Int =
    offset + 4
    random() // 4

    View Slide

  113. @ToddGinsberg
    FUNctions - Default Values
    fun random(offset: Int = 0): Int =
    offset + 4
    random() // 4
    random(1) // 5

    View Slide

  114. @ToddGinsberg
    FUNctions - Adding Parameters
    fun combine(first: Int,
    second: Int): Int =
    first + second

    View Slide

  115. @ToddGinsberg
    FUNctions - Adding Parameters
    fun combine(first: Int,
    second: Int,
    third: Int = 0): Int =
    first + second + third

    View Slide

  116. @ToddGinsberg
    FUNctions - Adding Parameters
    fun combine(first: Int,
    second: Int,
    third: Int = 0): Int =
    first + second + third
    combine(1, 2)
    combine(1, 2, 3)

    View Slide

  117. @ToddGinsberg
    FUNctions - Named Parameters
    fun combine(first: Int,
    second: Int): Int =
    first + second

    View Slide

  118. @ToddGinsberg
    FUNctions - Named Parameters
    fun combine(first: Int,
    second: Int): Int =
    first + second
    combine(1, 2) // 3

    View Slide

  119. @ToddGinsberg
    FUNctions - Named Parameters
    fun combine(first: Int,
    second: Int): Int =
    first + second
    combine(first = 1, second = 2) // 3

    View Slide

  120. @ToddGinsberg
    FUNctions - Named Parameters
    fun combine(first: Int,
    second: Int): Int =
    first + second
    combine(second = 2, first = 1) // 3

    View Slide

  121. @ToddGinsberg
    FUNction Names
    @Test
    fun replicantsHaveAFourYearLifespan() {
    // ...
    }

    View Slide

  122. @ToddGinsberg
    FUNction Names
    @Test
    fun `Replicants have a four year lifespan!`() {
    // ...
    }

    View Slide

  123. @ToddGinsberg
    Operator Overloading
    Limited to predefined operators!
    Expression Translated to
    a + b a.plus(b)
    a - b a.minus(b)
    a * b a.times(b)
    a / b a.div(b)
    a % b a.rem(b)
    a..b a.rangeTo(b)

    View Slide

  124. @ToddGinsberg
    Operator Overloading
    Limited to predefined operators!
    Expression Translated to
    a in b b.contains(a)
    a !in b !b.contains(a)

    View Slide

  125. @ToddGinsberg
    Operator Overloading
    Limited to predefined operators!
    Expression Translated to
    a[i] a.get(i)
    a[i] = b a.set(i, b)

    View Slide

  126. @ToddGinsberg
    Operator Overloading
    Limited to predefined operators!
    Expression Translated to
    a += b a.plusAssign(b)
    a -= b a.minusAssign(b)
    a *= b a.timesAssign(b)
    a /= b a.divAssign(b)
    a %= b a.remAssign(b)

    View Slide

  127. @ToddGinsberg
    Operator Overloading
    Limited to predefined operators!
    Expression Translated to
    a > b a.compareTo(b) > 0
    a < b a.compareTo(b) < 0
    a >= b a.compareTo(b) >= 0
    a <= b a.compareTo(b) <= 0

    View Slide

  128. @ToddGinsberg
    Extension FUNctions
    // Java
    public static boolean isEven(int i) {
    return i % 2 == 0;
    }

    View Slide

  129. @ToddGinsberg
    Extension FUNctions
    // Kotlin
    fun Int.isEven(): Boolean =
    this % 2 == 0

    View Slide

  130. @ToddGinsberg
    Extension FUNctions
    // Kotlin
    fun Int.isEven(): Boolean =
    this % 2 == 0
    2.isEven() // True!

    View Slide

  131. @ToddGinsberg
    The use Extension
    // Java
    try (Connection conn = getConnection()) {
    // ...
    }

    View Slide

  132. @ToddGinsberg
    The use Extension
    // Kotlin
    getConnection().use { conn ->
    // ...
    }

    View Slide

  133. @ToddGinsberg
    The apply Extension
    // Expression and Statements
    val p = Person()
    p.name = "Dr. Robert Hume"
    p.age = 47

    View Slide

  134. @ToddGinsberg
    The apply Extension
    // Single Expression
    val p = Person().apply {
    name = "Dr. Robert Hume"
    age = 47
    }

    View Slide

  135. @ToddGinsberg
    The apply Extension
    // Single Expression
    val p = Person().apply {
    name = "Dr. Robert Hume"
    age = 47
    address = Address().apply {
    line1 = "5 Tall Cedar Rd."

    }
    }

    View Slide

  136. @ToddGinsberg
    Higher-Order Functions
    fun measureTimeMillis(block: () -> Unit): Long {
    val start = System.currentTimeMillis()
    block()
    return System.currentTimeMillis() - start
    }

    View Slide

  137. @ToddGinsberg
    Higher-Order Functions
    fun measureTimeMillis(block: () -> Unit): Long {
    val start = System.currentTimeMillis()
    block()
    return System.currentTimeMillis() - start
    }
    val time = measureTimeMillis {
    someSlowQuery()
    }

    View Slide

  138. @ToddGinsberg
    One More Thing on FUNctions
    ● Functions are final by default.

    View Slide

  139. @ToddGinsberg
    One More Thing on FUNctions
    ● Functions are final by default.
    ● Arguments are always final.

    View Slide

  140. @ToddGinsberg
    One More Thing on FUNctions
    ● Functions are final by default.
    ● Arguments are always final.
    ● Functions can be defined in a file, outside of a class.

    View Slide

  141. @ToddGinsberg
    One More Thing on FUNctions
    ● Functions are final by default.
    ● Arguments are always final.
    ● Functions can be defined in a file, outside of a class.
    ● Functions can be defined within another function.

    View Slide

  142. @ToddGinsberg
    One More Thing on FUNctions
    ● Functions are final by default.
    ● Arguments are always final.
    ● Functions can be defined in a file, outside of a class.
    ● Functions can be defined within another function.
    ● Kotlin supports tail recursive functions.

    View Slide

  143. @ToddGinsberg
    What About Checked Exceptions?
    ?

    View Slide

  144. @ToddGinsberg
    What About Checked Exceptions?
    NO

    View Slide

  145. @ToddGinsberg
    Lambdas
    listOf(1, 2, 3, 4)
    .filter { x -> x % 2 == 0 }
    .map { y -> y * 2 }
    // List[4, 8]

    View Slide

  146. @ToddGinsberg
    Lambdas
    listOf(1, 2, 3, 4)
    .filter { x -> x % 2 == 0 }
    .map { y -> y * 2 }
    // List[4, 8]

    View Slide

  147. @ToddGinsberg
    Lambdas
    listOf(1, 2, 3, 4)
    .filter { it % 2 == 0 }
    .map { it * 2 }
    // List[4, 8]

    View Slide

  148. @ToddGinsberg
    Type Aliases
    fun doSomethingWithMap(
    ops: Map>
    ) {
    ops.entries // ...
    }

    View Slide

  149. @ToddGinsberg
    Type Aliases
    fun doSomethingWithMap(
    ops: Map>
    ) {
    ops.entries // ...
    }

    View Slide

  150. @ToddGinsberg
    Type Aliases
    typealias Operations = Map>
    fun doSomethingWithMap(ops: Operations) {
    ops.entries // …
    }

    View Slide

  151. @ToddGinsberg
    Import Aliases
    import java.util.Date
    import java.sql.Date

    View Slide

  152. @ToddGinsberg
    Import Aliases
    import java.util.Date as UtilDate
    import java.sql.Date as SqlDate

    View Slide

  153. @ToddGinsberg
    Import Aliases
    import java.util.Date as SqlDate
    import java.sql.Date as UtilDate

    View Slide

  154. @ToddGinsberg
    Import Aliases
    import java.util.Date as SqlDate
    import java.sql.Date as UtilDate

    View Slide

  155. @ToddGinsberg
    Reified Generics
    // Ugly
    val log = Logger.getLogger(Metrics::class.java)
    // What I want
    val log = loggerOf()

    View Slide

  156. @ToddGinsberg
    Reified Generics
    // Thanks type erasure :(
    fun loggerOf(): Logger =
    Logger.getLogger(T::class.java)

    View Slide

  157. @ToddGinsberg
    Reified Generics
    inline fun loggerOf(): Logger =
    Logger.getLogger(T::class.java)

    View Slide

  158. @ToddGinsberg
    Reified Generics
    inline fun loggerOf(): Logger =
    Logger.getLogger(T::class.java)
    // Works!
    val log = loggerOf()

    View Slide

  159. @ToddGinsberg
    So Many Things Left Out! :(
    - Companion objects
    - Massive STDLIB replaces your util classes
    - Inline classes
    - Unsigned integers
    - Multiplatform
    - Coroutines
    - DSL support
    - Contracts
    - Infix functions

    View Slide

  160. @ToddGinsberg
    Summary

    View Slide

  161. @ToddGinsberg
    Make “Bad” Choices Explicit or Impossible
    Fixed in Kotlin:
    - Singleton support built in
    - Override keyword mandatory
    - Properties over fields with getter/setter
    - Mutability is minimized (val + collections)
    - Inheritance prohibited by default
    - Builders are easy with default values
    - No checked exceptions
    - Structural equality is the same everywhere
    - Delegation support makes composition easier

    View Slide

  162. @ToddGinsberg
    Why Do I Use Kotlin?
    ● I write far less code

    View Slide

  163. @ToddGinsberg
    Why Do I Use Kotlin?
    ● I write far less code
    ● The code I write is more expressive and clear

    View Slide

  164. @ToddGinsberg
    Why Do I Use Kotlin?
    ● I write far less code
    ● The code I write is more expressive and clear
    ● I avoid whole classes of defects

    View Slide

  165. @ToddGinsberg
    Why Do I Use Kotlin?
    ● I write far less code
    ● The code I write is more expressive and clear
    ● I avoid whole classes of defects
    ● Allows me to write in a more functional style

    View Slide

  166. @ToddGinsberg
    Why Do I Use Kotlin?
    ● I write far less code
    ● The code I write is more expressive and clear
    ● I avoid whole classes of defects
    ● Allows me to write in a more functional style
    ● Plays well with the tools I use (Spring, IDEA, Gradle)

    View Slide

  167. @ToddGinsberg
    Why Do I Use Kotlin?
    ● I write far less code
    ● The code I write is more expressive and clear
    ● I avoid whole classes of defects
    ● Allows me to write in a more functional style
    ● Plays well with the tools I use (Spring, IDEA, Gradle)
    ● Writing Kotlin, for me, is more fun

    View Slide

  168. @ToddGinsberg
    Quantified Fun!
    % of developers who are developing with the
    language or technology but have not
    expressed interest in continuing to do so
    (Stack Overflow Developer Survey 2019)

    View Slide

  169. @ToddGinsberg
    Quantified Fun!
    % of developers who are developing with the
    language or technology but have not
    expressed interest in continuing to do so
    (Stack Overflow Developer Survey 2019)
    Kotlin

    View Slide

  170. @ToddGinsberg
    Want to Learn More?
    https://play.kotlinlang.org

    View Slide

  171. @ToddGinsberg
    Want to Learn More?
    https://www.coursera.org/learn/kotlin-for-java-developers

    View Slide

  172. #springone
    @s1p
    @ToddGinsberg
    [email protected]
    https://todd.ginsberg.com
    Thank You!

    View Slide