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

Introduction to Kotlin for Java Developers

Introduction to Kotlin for Java Developers

Introduction to Kotlin for Java Developers

Anton Sitnikov, Chief Software Architect, Exactpro

DEV Meetup, Kostroma
1 November 2019

Video: https://youtu.be/2AtcIr-qBKs

To learn more about Exactpro, visit our website https://exactpro.com/
EXTENT Conference website: https://extent.exactpro.com/
Follow us on
LinkedIn https://www.linkedin.com/company/exactpro-systems-llc
Twitter https://twitter.com/exactpro
Facebook https://www.facebook.com/exactpro/
Instagram https://www.instagram.com/exactpro/

Subscribe to Exactpro Vimeo channel https://vimeo.com/exactproSubscribe to Exactpro YouTube channel https://www.youtube.com/c/exactprosystems

5206c19df417b8876825b5561344c1a0?s=128

Exactpro
PRO

November 01, 2019
Tweet

Transcript

  1. Build Software to Test Software exactpro.com Введение в Kotlin для

    разработчиков Java Date: November 01, 19 Author: Anton Sitnikov
  2. 2 Build Software to Test Software exactpro.com Kotlin • Язык

    для JVM • Назван в честь острова под Санкт-Петербургом • Начал разрабатываться в 2011 году как альтернатива Scala • В 2019 рекомендован Google как язык для написания приложений для Android
  3. 3 Build Software to Test Software exactpro.com Null pointers in

    Java Connection connection; void open() { connection = … } void addBlueberryCake() { connection.execute(“INSERT ...”); }
  4. 4 Build Software to Test Software exactpro.com Nullable типы в

    Kotlin Connection connection; // = null // Java var connection: Connection // Kotlin - ошибка var connection: Connection? = null lateinit var connection: Connection
  5. 5 Build Software to Test Software exactpro.com Null pointers in

    Kotlin var connection: Connection? = null fun open() { connection = … } fun addBlueberryCake() { connection?.execute(“INSERT ...”) }
  6. 6 Build Software to Test Software exactpro.com Null pointers in

    Kotlin fun addBlueberryCake() { (connection ?: throw IllegalState(“connection isn’t open”)) .execute(“INSERT ...”) }
  7. 7 Build Software to Test Software exactpro.com Null pointers in

    Kotlin lateinit var connection: Connection fun addBlueberryCake() { connection.execute(“INSERT ...”) }
  8. 8 Build Software to Test Software exactpro.com Оператор сравнения в

    Kotlin aName == “Name” // использует equals() aName === “Name” // сравнивает по ссылке
  9. 9 Build Software to Test Software exactpro.com Java beans in

    Kotlin // Java int getValue() … setValue(int iValue) … // Kotlin val iValue = myObject.value myObject.value = iValue
  10. 10 Build Software to Test Software exactpro.com String interpolation “$sellerName

    sold ${basket.size} items to $buyerName”
  11. 11 Build Software to Test Software exactpro.com Default parameters fun

    collectCollectibles( source: Database, timeout: Duration = 5.minutes) collectCollectibles( source = bobsVault, timeout = 1.seconds)
  12. 12 Build Software to Test Software exactpro.com Extension methods val

    Int.minutes: Duration get() { return Duration.ofMinutes(toLong()) }
  13. 13 Build Software to Test Software exactpro.com Delegated Properties val

    connection: Connection by lazy { connect() } val login: String by systemProperty(“LOGIN”)
  14. 14 Build Software to Test Software exactpro.com Lambda как последний

    параметр функции perimeter = edges.sumBy({ edge -> edge.length}) perimeter = edges.sumBy { edge -> edge.length} perimeter = edges.sumBy { it.length }
  15. 15 Build Software to Test Software exactpro.com 'try/catch' expression val

    result = try { count() } catch (e: ArithmeticException) { null }
  16. 16 Build Software to Test Software exactpro.com Остальное одной строкой

    • var / val • перегрузка операторов / infix функции • reified параметры шаблона • with, let, also, ...
  17. 17 Build Software to Test Software exactpro.com Подробней о Kotlin

    TMPA 2015 TMPA Kotlin
  18. 18 Build Software to Test Software exactpro.com Вопросы?

  19. 19 Build Software to Test Software exactpro.com Спасибо!