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

Kotlin Introduction

Kotlin Introduction

Slides for my introduction to Kotlin talk - find the sample code over here https://github.com/techdev-solutions/kotlin-spring-demo

Alexander Hanschke

July 26, 2017
Tweet

Other Decks in Programming

Transcript

  1. #kotlin @alexhanschke OHAI @alexhanschke •  working at techdev Solu6ons GmbH

    in Berlin •  doing Java since 2008, and •  Kotlin since 2016
  2. #kotlin @alexhanschke Goals •  give a gentle introduc6on •  cover

    most relevant features (breath, not depth) •  whet your appe6te •  no prerequisites; Java knowledge helps
  3. #kotlin @alexhanschke Agenda •  History •  Func6ons •  Expressions • 

    Classes & Types •  Extensions •  Null-safety •  Demo
  4. #kotlin @alexhanschke Kotlin.. •  is a sta6cally-typed language •  compiles

    to Java bytecode, JavaScript, or machine code (Kotlin/Na6ve) •  is open source
  5. #kotlin @alexhanschke Why Kotlin? •  JetBrains used Java in most

    of their tools •  need for a language that is concise, expressive, toolable, interoperable and pragma6c •  Java too slow to catch up •  Scala had the right features, but compiler was too slow
  6. #kotlin @alexhanschke Func-ons class Person { fun greet() { println("hello!")

    } } fun main(args: Array<String>) { Person().greet() }
  7. #kotlin @alexhanschke Func-ons fun block(): String { return "return type

    required" } fun expression() = "return type inferred"
  8. #kotlin @alexhanschke Func-ons fun url(protocol: String, domain: String, port: Int):

    String { return "$protocol: //$domain:$port" } url("http", "example.com", 80) url(domain = "example.com", port = 80, protocol = "http")
  9. #kotlin @alexhanschke Func-ons fun url(protocol: String = "http", domain: String,

    port: Int): String { return "$protocol: //$domain:$port" } url(domain = "example.com", port = 80)
  10. #kotlin @alexhanschke Func-ons File: playground.kt fun url(protocol: String = "http",

    domain: String, port: Int): String { return "$protocol: //$domain:$port" } File: App.java public class App { public static void main(String ... args) { PlaygroundKt.url("http", "example.com", 80); } }
  11. #kotlin @alexhanschke Func-ons File: playground.kt @file:JvmName("Playground") fun url(protocol: String =

    "http", domain: String, port: Int): String { return "$protocol: //$domain:$port" } File: App.java public class App { public static void main(String ... args) { Playground.url("http", "example.com", 80); } }
  12. #kotlin @alexhanschke Func-ons File: playground.kt @file:JvmName("Playground") @JvmOverloads fun url(protocol: String

    = "http", domain: String, port: Int): String { return "$protocol: //$domain:$port" } File: App.java public class App { public static void main(String ... args) { Playground.url("example.com", 80); } }
  13. #kotlin @alexhanschke Variables val even: (Int) -> Boolean = {

    it % 2 == 0 } fun test(predicate: (Int) -> Boolean, candidate: Int) { if (!predicate(candidate)) { throw IllegalArgumentException() } } fun main(args: Array<String>) { test(even, 2) }
  14. #kotlin @alexhanschke Variables typealias IntPredicate = (Int) -> Boolean val

    even: IntPredicate = { it % 2 == 0 } fun test(predicate: IntPredicate, candidate: Int) { if (!predicate(candidate)) { throw IllegalArgumentException() } } fun main(args: Array<String>) { test(even, 2) }
  15. #kotlin @alexhanschke Expressions fun boom() { throw IOException("boom!") } Meanwhile

    in Java .. public static void main(String ... args) { try { Playground.boom(); } catch (IOException cause) { ..} } Does not compile IOException is never thrown in that block
  16. #kotlin @alexhanschke Expressions @Throws(IOException ::class) fun boom() { throw IOException("boom!")

    } Meanwhile in Java .. public static void main(String ... args) { try { Playground.boom(); } catch (IOException cause) { ..} }
  17. #kotlin @alexhanschke Expressions fun status(code: Int) = when (code) {

    in 100 ..199 -> "info" in 200 ..299 -> "success" in 300 ..399 -> "redirect" in 400 ..499 -> "client error" in 500 ..599 -> "server error" else -> "unknown" }
  18. #kotlin @alexhanschke Expressions fun status(code: Int) = when { code

    in 100 ..199 -> "info" code in 200 ..299 -> "success" code in 300 ..399 -> "redirect" code in 400 ..499 -> "client error" code in 500 ..599 -> "server error" else -> "unknown" }
  19. #kotlin @alexhanschke Classes public class Commit {( ( private String

    hash;( private String author;( ( public String getAuthor() {( return author;( }( ( public void setAuthor(String author) {( this.author = author;( }( ( public String getHash() {( return hash;( }( ( public void setHash(String hash) {( this.hash = hash;( }( } class Commit(val hash: String, var author: String) val commit = Commit("abc123", "alexhanschke") commit.author = "john.doe" Defaults All classes are public and final by default.
  20. #kotlin @alexhanschke Classes @lombok.Data public class Commit {( ( private

    String hash;( private String author; ( } / ** •  toString() •  equals() •  hashCode() •  Getter & Setter */ data class Commit(val hash: String, var author: String) val one = Commit("abc123", "alexhanschke") val two = one.copy(hash = "xyz123")
  21. #kotlin @alexhanschke Classes abstract class Shape class Rectangle : Shape()

    { fun area(): Double { ..} } class Square : Rectangle() { override fun area(): Double { ..} }
  22. #kotlin @alexhanschke Classes abstract class Shape open class Rectangle :

    Shape() { open fun area(): Double { ..} } class Square : Rectangle() { override fun area(): Double { ..} }
  23. #kotlin @alexhanschke Extension Func-ons fun Int.squared() = this * this

    3.squared() // 9 fun List<String>.random(): String { if (this.isEmpty()) { throw IllegalStateException("List must not be empty.") } return this[Random().nextInt(this.size)] } listOf("a", "b", "c").random()
  24. #kotlin @alexhanschke Extension Func-ons fun Int.squared() = this * this

    3.squared() // 9 fun List<String>.random(): String { if (this.isEmpty()) { throw IllegalStateException("List must not be empty.") } return this[Random().nextInt(this.size)] } Playground.random(List<String> receiver)
  25. #kotlin @alexhanschke Extension Func-ons •  extension func6ons allow adding behavior

    to exis6ng types •  sta6cally resolved •  don't break encapsula6on •  member func6ons take precedence
  26. #kotlin @alexhanschke Extension Proper-es class Person(val name: String) val Person.email:

    String get() = "[email protected]" Person("alex").email // [email protected] "text goes here" ::class // kotlin.String "text goes here" ::class.java // java.lang.String
  27. #kotlin @alexhanschke Null-safety class Commit(val hash: String, var author: String?)

    fun main(args: Array<String>) { val commit: Commit? = null println(commit.author) // Compiler: err, no. }
  28. #kotlin @alexhanschke Null-safety class Commit(val hash: String, var author: String?)

    val commit: Commit? = null if (commit != null) { if (commit.author != null) { commit.author.reversed() } } Smart Casts The compiler automagically casts a given type when applicable.
  29. #kotlin @alexhanschke Null-safety class Commit(val hash: String, var author: String?)

    val commit: Commit? = null val name = commit?.author ?: "elvis"
  30. #kotlin @alexhanschke Null-safety class Commit(val hash: String, var author: String?)

    val commit: Commit? = null val name = commit?.author ?: throw IllegalArgumentException("author required")
  31. #kotlin @alexhanschke Null-safety class Commit(val hash: String, var author: String?)

    val commit: Commit? = null commit !!.author !!.reversed()
  32. #kotlin @alexhanschke Resources -  www.kotlinweekly.net -  h[ps://kotlin.link/ -  h[ps://github.com/techdev-solu6ons/janitor - 

    h[ps://jaxenter.de/kotlin-tutorial-48156 -  h[ps://jaxenter.de/kotlin-ein-tutorial-fuer-einsteiger-teil-2-48587 -  h[ps://jaxenter.de/kotlin-ein-tutorial-fuer-einsteiger-teil-3-48967 -  h[ps://jaxenter.de/kotlin-ein-tutorial-fuer-einsteiger-teil-4-49160