Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

From Java to Kotlin - The first month in practice

376 views

Published on

This presentation shows the advantages and how easy it is for a Java developer to switch to Kotlin. There is also a sample project on github, that compares the two languages at the example of a Spring Boot application: https://github.com/Meshcloud/spring-kotlin-example

There is also a second version of these slides available on slideshare, as slideshare does not allow updating existing slides: https://www.slideshare.net/StefanTomm/from-java-to-kotlin-the-first-month-in-practice-v2

Published in: Software
  • Login to see the comments

  • Be the first to like this

From Java to Kotlin - The first month in practice

  1. 1. From Java to Kotlin The first months in practice 15. Nov 2017
  2. 2. Agenda What to expect today? My Background Why Kotlin? Live-Coding: Convert Spring Boot App Summary
  3. 3. My Background solution-oriented developer >10 years Java 3.5 month @ Meshcloud
  4. 4. Why Kotlin? Expressive 100% Java interoperability Developed by JetBrains, Open Source -> great IDE support Strictly-Typed -> Null-Safety Practical, not academic
  5. 5. Demo time Let’s get practical :) https://github.com/Meshcloud/spring-kotlin-example
  6. 6. Demo Statistics LoC Java: 460 Kotlin: 252 Reduction: 46% LoC without data classes Java: 232 Kotlin: 185 Reduction: 21% https://github.com/Meshcloud/spring-kotlin-example
  7. 7. Summary All the cool things we’ve just seen, and a bit more, on a few slides
  8. 8. 1. Null-Safety Null checks during compilation: var x = “value” x = null // compile error Great support for handling nullable values: var x:MyClass? val child = x?.child Intelligent compiler: if (x != null) { val child = x.child } Elvis Operator: projectRepository.findOne(id) ?: throw NotFoundException() Force not null: val y: String = x!!
  9. 9. 2. Mighty little helpers String templates: val text = "Project ${project.name} - ${project.description}" Ranges for (i in 1..100) for (i in 10 downTo 1) Structural equality with == project == otherProject // calls null-safe equals project === otherProject // referential equality Destructuring for ((key, value) in map) { print("Key: $key, Value: $value") }
  10. 10. 3. Avoid property boilerplate Data classes (with equals, hashcode, toString & clone): data class Project( val name: String, val description: String, val fullText: String get() = “$name - $description” ) Implicit Getters/Setters & property access: class Project { var name: String } Project().name
  11. 11. 4. Expressive arguments Default Arguments: fun create( name: String, description: String = “default” ) Named Arguments create( name = “My Project”, description = “Some details” ) Apply method: Project().apply( name = “My Project” description = “Some details” )
  12. 12. 5. Higher-Order Functions Java (Function, Consumer, Supplier, BiConsumer, …): public String myFunction( BiFunction<String, String, String> fn ) fn.apply(“1”, “2”) // .get() on Supplier, ... Kotlin: fun myFunction( fn: (String, String) -> String ) fn(“1”, “2”)
  13. 13. 6. Concise Streaming API Java: projectRepository .findAll() .stream() .map(p -> new ProjectListDTO(p)) .collect(Collectors.toList()); Kotlin: projectRepository .findAll() .map { ProjectListDTO(it)}
  14. 14. 7. Reduce the overhead Type Inference: Map<ResourceType, List<Resource>> resourceMapByType = service.findResourceMapByType(); // Java val resourceMapByType = service.findResourceMapByType() // Kotlin Smart Casts: if (obj is Project) { print(obj.description) // obj is now known to be a Project } When Expressions: when { "Bitcoin" in projectNames -> println("Bad guy!") projectNames is empty -> println("No Projects") else -> println("Just normal projects!") }
  15. 15. 8. Further concepts in Kotlin Coroutines: // experimental simplified non-blocking IO async implementation async { doSomething(foo) ... } Operator Overloading: data class Vec(val x: Float, val y: Float) { operator fun plus(v: Vec) = Vec(x + v.x, y + v.y) } val v = Vec(2f, 3f) + Vec(4f, 1f)
  16. 16. Questions? Contacts Stefan Tomm <stomm@meshcloud.io> Johannes Rudolph <jrudolph@meshcloud.io> www.meshcloud.io @meshstack @meshstack https://kotlinlang.org/

×