Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Kotlin – Alternative oder Ergänzung zu Java?

71 Aufrufe

Veröffentlicht am

Vortrag von Robin Roschlau, Deutsche Post Adress GmbH & Co. KG, auf dem Expertenkreis Java, GEDOPLAN GmBH, 01.02.2018

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Kotlin – Alternative oder Ergänzung zu Java?

  1. 1. Haha, er hat „Kot“ gesagt 13.09.2017 Robin Roschlau This is a hand-out version of the talk held on Feb 01 2018 at the Gedoplan Expertenkreis. Most of the slides contain additional info in the notes that was delivered verbally in the talk.
  2. 2. About me 2 ? Robin Roschlau
  3. 3. Agenda 3 What is Kotlin? What makes it so special? Real world experience
  4. 4. What is this „Kotlin“? 4 Kotlin is a statically typed language that targets the JVM and JavaScript. It is a general-purpose language intended for industry use.„ “
  5. 5. What is this „Kotlin“? 5 Kotlin is a statically typed language that targets the JVM and JavaScript. It is a general-purpose language intended for industry use.„ “ 2010
  6. 6. What is this „Kotlin“? 6 Kotlin is a statically typed language that targets the JVM and JavaScript. It is a general-purpose language intended for industry use.„ “ 2010 2012
  7. 7. What is this „Kotlin“? 7 Kotlin is a statically typed language that targets the JVM and JavaScript. It is a general-purpose language intended for industry use.„ “ 2010 2012 2016 v1.0
  8. 8. What is this „Kotlin“? 8 Kotlin is a statically typed language that targets the JVM and JavaScript. It is a general-purpose language intended for industry use.„ “ 2010 2012 20172016 v1.0
  9. 9. Type system lacking Java interop lacking Really? Another JVM language?  Fixes a lot of Java‘s shortcomings (Null-Safety, Type Inference, Properties, …)  Concise syntax, easy to read and write for developers used to Java  Completely interoperable  No compromises in compilation speed or tooling support 4
  10. 10. Show me some code! Live demo Code here: https://github.com/roschlau/kotlin-demo-jug Online IDE with a tour through the language: https://try.kotlinlang.org/ 10
  11. 11. So, what makes Kotlin so special? 11
  12. 12. So, what makes Kotlin so special? 12
  13. 13. Interoperability 13 Everything from Java is usable from Kotlin and vice versa  Inherit in one language from a class in the other language  Use all the libraries you‘re used to  Do some things in Kotlin, others in Java Mix and match to your heart’s content. As long as you don’t put Java and Kotlin code in the same file, anything goes.
  14. 14. Show me more code! Live demo 14
  15. 15. Tooling 15  Automatic project setup  Automatic conversion of Java files to Kotlin  Copy-Paste conversion  View Kotlin bytecode and decompile to equivalent Java code  REPL  Compilation speed is comparable to Java*
  16. 16. Let’s get real Experiences from real world projects 16
  17. 17. Let’s get real 17  Huge legacy JavaEE application  Kotlin configured in 2 min 0 100 200 300 400 500 600 700 800 900 1000 Random DTO Random DAO Random Util Class FieldChecks ArticleSearchParameter Java Kotlin -45% -10% -15% -26% -66% LOC
  18. 18. Adoption effort 18 • Getting Projects ready • New project: No overhead • Existing projects: < 15 min • Working out guidelines • How and what to migrate • Kotlin Code Style • Treatment of new and possibly controversial features (Exceptions, extension functions, operator overloading, …) • Building developer know-how • Up-front training: ~1 • Getting comfortable: ~1 week
  19. 19. Gotchas 19 • Functions and classes are final by default. • Mockito 2 needed to mock final classes • Bean proxying: All-Open plugin • No checked Exceptions • New policy on exception handling needed • No official SonarQube plugin yet • JB are working on it, no ETA • 3rd party plugins are available • No easy way back • Conversion from Java to Kotlin is easy and fun. The reverse, not so much.
  20. 20. Where do I start? • http://try.kotlinlang.org • Auto-setup a project with IntelliJ or Android Studio • Check out the language docs and community:  http://kotlinlang.org/docs/reference/  http://kotlinlang.org/community/  https://kotlinlang.slack.com  https://stackoverflow.com/questions/tagged/kotlin 20
  21. 21. The end Questions? Advanced topics: - Property Delegation - Generics - Operator Overloading - Concurrency - Android Utilities 21 Contact: robin.roschlau@gmail.com
  22. 22. Resources 22 Official Stuff from JetBrains Kotlin Language Reference http://kotlinlang.org/docs/reference/ Jetbrains Motivation for developing Kotlin https://blog.jetbrains.com/kotlin/2011/08/why-jetbrains-needs-kotlin/ Online IDE with loads of tasks and puzzles teaching the core language features http://try.kotlinlang.org/ Kotlin FAQ https://kotlinlang.org/docs/reference/faq.html Kotlin Cheatsheet https://gist.github.com/dodyg/5823184 On the Rise of Kotlin https://blog.heroku.com/rise-of-kotlin Kotlin vs. Scala: Which Problems do they solve? https://superkotlin.com/kotlin-vs-scala/ Real world field reports Kotlin for grumpy Java developers https://medium.com/@Pinterest_Engineering/kotlin-for-grumpy-java-developers-8e90875cb6ab Business implications of adopting Kotlin https://www.corda.net/2017/01/10/kotlin/ The case against Kotlin https://medium.com/@Pinterest_Engineering/the-case-against-kotlin-2c574cb87953 Lessons learned from converting an Android App to Kotlin https://m.signalvnoise.com/how-we-made-basecamp-3s-android-app-100-kotlin-35e4e1c0ef12
  23. 23. if (durationCode.equals(Duration.H.getCode()) || durationCode.equals(Duration.HM.getCode())) { durationDate.add(Calendar.HOUR_OF_DAY, 1); } else if (durationCode.equals(Duration.D.getCode()) || durationCode.equals(Duration.DM.getCode())) { durationDate.add(Calendar.DAY_OF_YEAR, 1); } else if (durationCode.equals(Duration.D3.getCode())) { durationDate.add(Calendar.DAY_OF_YEAR, 3); } else if (durationCode.equals(Duration.W.getCode()) || durationCode.equals(Duration.WM.getCode())) { durationDate.add(Calendar.WEEK_OF_YEAR, 1); } else if (durationCode.equals(Duration.M.getCode()) || durationCode.equals(Duration.MM.getCode())) { durationDate.add(Calendar.MONTH, 1); } else if (durationCode.equals(Duration.MP.getCode())) { durationDate.add(Calendar.MONTH, 1); } else if (durationCode.equals(Duration.Y.getCode()) || durationCode.equals(Duration.YM.getCode())) { durationDate.add(Calendar.YEAR, 1); } else { throw new DurationCodeException("Duration [Code: " + durationCode + "] not found."); } When-Statement in practice 23 when (durationCode) { Duration.H.code, Duration.HM.code -> durationDate.add(Calendar.HOUR_OF_DAY, 1) Duration.D.code, Duration.DM.code -> durationDate.add(Calendar.DAY_OF_YEAR, 1) Duration.D3.code -> durationDate.add(Calendar.DAY_OF_YEAR, 3) Duration.W.code, Duration.WM.code -> durationDate.add(Calendar.WEEK_OF_YEAR, 1) Duration.M.code, Duration.MM.code -> durationDate.add(Calendar.MONTH, 1) Duration.MP.code -> durationDate.add(Calendar.MONTH, 1) Duration.Y.code, Duration.YM.code -> durationDate.add(Calendar.YEAR, 1) else -> throw DurationCodeException( "Duration [Code: $durationCode] not found.") }
  24. 24. public void foo(String bar) { int len = bar.length(); } Null-Safety (Goodbye NPE, we won‘t miss you!) 24 NPE waiting to happen Compilation Error fun foo(bar: String) { } fun foo(bar: String?) { } val len = bar.length if(bar != null) { val len = bar.length } val len: Int? = bar?.length val len: Int = bar!!.length val bar2: String? = bar val bar2: String = bar!! val bar2: String = bar ?: "String was null"
  25. 25. Operator Overloading 25 class RandomClass { operator fun plus(s: String): RandomClass {…} override fun equals(other: Any?): Boolean {…} operator fun compareTo(other: RandomClass): Int {…} operator fun get(i: Int): Any {…} operator fun set(i: Int, value: Any) {…} operator fun contains(i: Any): Boolean {…} operator fun invoke(p: Int) {…} } val r1 = RandomClass() val r2 = RandomClass() val r2 = r1 + "Test" if(r1 == r2) {…} if(r1 > r2) {…} val a = r1[2] r1[42] = "Test" if("Test" in r1) {…} r1(100)
  26. 26. Kotlin for Android 26 override fun onCreate(savedInstanceState: Bundle?) { removeButton.onClick { alert(R.string.remove_item_dialog_title, R.string.remove_item_dialog_body) { positiveButton(R.string.remove) { removeItem() } negativeButton(R.string.cancel) }.show() } } fun removeItem() { doAsync { apiClient.removeItem() uiThread { toast(R.string.item_removed_confirmation) } } }

×