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

Kotlin from scratch

Kotlin from scratch

Do you want to learn #Kotlin programming language from scratch? This is the first episode of my simple course.

Franco Lombardo

May 04, 2020
Tweet

More Decks by Franco Lombardo

Other Decks in Programming

Transcript

  1. What is Kotlin? Kotlin is a programming language with a

    syntax similar to C / Java / JavaScript / C# • Strong typing (a number is not a string) • Static typing (types of variables are checked at compile time) • Type inference (type declaration can often be omitted) • Object Oriented Programming (classes, objects, methods, inheritance, polymorphic calls) • Functional Programming Paradigm (functions are types, so we can pass them around)
  2. Who created Kotlin? • Kotlin was created by JetBrains, a

    company that builds programming languages IDEs and tools such as IntelliJ IDEA • There is a Kotlin Foundation created with Google to protect the trademarks and to design the development path • Open source licensed under Apache 2 • Kotlin is the name of an island near St. Petersburg (Java is the name of an island, too)
  3. When was Kotlin created? • 2010: Start of the Kotlin

    project • 2012: The project becomes Open Source • 2016: First stable release • 2017: Google announces support on Android • 2019: It becomes the preferred language for Android
  4. Where does Kotlin run? • JVM: primary target, the only

    one that is battle-proven • JS: browser and server (NodeJS) (early stage) • Native: iOS and more (early stage) Be careful: cross platform code cannot use Java libraries!
  5. Why Kotlin? • It’s modern (OOP + functional + coroutines

    +…) • It’s concise • It’s interoperable • it can reuse Java libraries ecosystem • it can be used by Java projects • It’s multiplatform (maybe?) • It handles null values in a safer way • Decoupling the evolution of the language from the evolution of the JVM (e.g. phones, AS400, mission critical production servers)
  6. Strong static typing with inference fun main() { val authorOfNabucco:

    String = "Giuseppe Verdi" val authorOfTurandot = "Giacomo Puccini" println("Italian composers: $authorOfNabucco and $authorOfTurandot") }
  7. Strong static typing with inference fun main() { val authorOfNabucco:

    String = "Giuseppe Verdi" val authorOfTurandot = "Giacomo Puccini" println("Italian composers: $authorOfNabucco and $authorOfTurandot") } Explicit type declaration Type inference String interpolation Immutable variables are preferred
  8. Kotlin is concise: why is it important? Concise ↓ Less

    boilerplate code ↓ Easier to understand (less cognitive overload) ↓ Fewer bugs
  9. How much is Kotlin concise? Data class: Java Bean with

    hashCode, toString, equals and much more
  10. How much is Kotlin concise? Decompiled Java version of the

    previous data class (just a part of it)
  11. Kotlin is safer Can be null Cannot be null val

    italianComposer: String = "Giuseppe Verdi" val dutchComposer: String? = null
  12. val result = openDatabase("franco", "secret") ?.findFirstComposerByNation("Italy") ?.findOperaByYear(1871) ?: "No results"

    Kotlin is safer Composing nullable values/functions in a safe way Possibly null Not null
  13. val result = openDatabase("franco", "secret") ?.findFirstComposerByNation("Italy") ?.findOperaByYear(1871) ?: "No results"

    Kotlin is safer Composing nullable values/functions in an expressive way “or else” “and then”
  14. Extension functions data class Database(val composers: List<Composer>) data class Composer(val

    name: String, val nation: String, val operas: List<Opera>) data class Opera(val name: String, val yearOfComposition: Int) fun Database.findFirstComposerByNation(nation: String): Composer? = this.composers.firstOrNull { it.nation == nation } fun Composer.findOperaByYear(year: Int): Opera? = this.operas.firstOrNull { it.yearOfComposition == year } Extending classes from outside
  15. The power of lambdas val elapsedTime = measureTimeMillis { execute(compilationUnit.main.stmts)

    } public fun measureTimeMillis(block: () -> Unit): Long { val start = System.currentTimeMillis() block() return System.currentTimeMillis() - start }
  16. References Kotlin in action Dmitry Jemerov, Svetlana Isakova Kotlin for

    Java Developers https://www.coursera.org/learn/kotlin-for-java-developers JetBrains Kotlin Academy https://hyperskill.org/onboarding/?track=kotlin JetBrains Kotlin playlist on YouTube https://bit.ly/kotlinTube Kotlin hands-on labs (Coroutines, JS, Native) https://play.kotlinlang.org/hands-on/overview