Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Hello :)

Slide 3

Slide 3 text

Together. Learn.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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,

Slide 6

Slide 6 text

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; } }

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Introduction to Kotlin

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Packages and Files

Slide 13

Slide 13 text

Variables

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Functions / Extension Functions

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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)

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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))

Slide 27

Slide 27 text

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))

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Null Safety

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Classes

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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; } }

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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; } }

Slide 43

Slide 43 text

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] } }

Slide 44

Slide 44 text

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) { (…) } }

Slide 45

Slide 45 text

Lambdas

Slide 46

Slide 46 text

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 }

Slide 47

Slide 47 text

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 }

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Questions? Almost done :)

Slide 51

Slide 51 text

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/

Slide 52

Slide 52 text

Thank you!