Kotlin Intro & Starting the Koans

Tomás Hanley

9th July 2018

What is Kotlin?

  • General purpose programming language
  • Statically typed
  • Targets the JVM
    • Can also compile to Javascript
    • Can also be compiled to native binaries
  • Developed by JetBrains

Kotlin History

  • Started in 2010 by JetBrains
  • They had a huge Java codebase
  • Wanted a modern expressive language
  • None of the existing languages were a good fit

Kotlin Design Goals

  • 100% Java Interoperability
  • More concise than Java
  • Less error prone than Java
  • Way simpler than Scala
  • Pragmatic
    • Not a research project
    • No ground breaking features
    • Best features established in other languages: groovy, scala, c#

"The primary goal of Kotlin is to provide a more concise, more productive, safer alternative to Java that’s suitable in all contexts where Java is used today"

Timeline

  • 2010: Work begins on Kotlin
  • 2011: Kotlin is open sourced
  • 2016: Kotlin 1.0 released 
  • 2017: Google announce Kotlin has first class support on Android.
  • 2018: Second most loved language in the StackOverflow Developer survey

Current adoption rate?

Why learn Kotlin?

Why learn Kotlin?

  • Kotlin is a better language for the JVM than Java
    • Really easy to get up and running
    • Simpler language than Java
    • Much safer than Java

      • No more NullPointerExceptions!

      • Streamlines the handling of null values

    • More more clean & concise than Java

    • 100% Interoperable with Java

      • Kotlin & Java classes can live side by side

Why learn Kotlin?

  • Awesome compilers for multiple platforms
    • ~99% can be compiled to Java 6 bytecode

    • Can be compiled to Javascript

    • Can be compiled to native binaries for iOS, OSX, Windows, Linux etc.

    • First class language for Android development

 

Why learn Kotlin?

  • Great functional programming features
    • Beautiful streams library
    • Immutability is the default
    • Final is the default
  • Extension functions - black magic
  • Lots of other great features

 

Who has read this?

Code Examples

Concise?

public class Person {

    private final String firstName;
    private final String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person person = (Person) 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;
    }

    @Override
    public String toString() {
        return "Person{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

Concise!

data class Person(
    val firstName: String, 
    val lastName: String
)

Safe?

public class MainJava {

    public static void main(String[] args) {
        
        Person player1 = new Person("Bob", null);
        Person player2 = new Person("Tomás", "Hanley");

        .
        .
        .
        .        

        if (player1.getLastName().equals(player2.getLastName())) {
            System.out.println("Last names equal! 😁");
        } else{
            System.out.println("Last names not equal 😭");
        }


    }

}

How does Kotlin handle this better?

IN

The Toast Stack

  • All in one restaurant management platform
  • Guest and restaurant-facing Android tablets
    • Java & Kotlin
  • Backend services
    • ​Java 8 Dropwizard Services
    • Kotlin being used as well
  • Internal, guest-facing and restaurant-facing web apps
    • ​Play monolith -> Angular/React

A series of exercises to get you familiar with the Kotlin Syntax

Koans Introduction

  1. Hello World
  2. Java to Kotlin conversion
  3. Named Arguments
  4. Default Arguments
  5. Lambdas
  6. Strings
  7. Data Classes
  8. Nullable Types
  9. Smart Casts
  10. Extension Functions
  11. Object Expressions

Getting set up

  • Highly recommend you use Intellij

    • Comes bundled with Kotlin & great tools

  • Install the EduTools plugin

    • Preferences -> Plugins -> Install Jetbrains plugins

    • Search EduTools, install & restart Intellij

    • Select Learner if prompted on restart

  • File > Browse courses and select Kotlin

  • Refresh your gradle dependencies

For anyone finished early

References

https://kotlinlang.org/

https://www.slideshare.net/intelliyole/kotlin-riviera-dev

https://speakerdeck.com/alexgherschon/introduction-to-kotlin

Made with Slides.com