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.

Kotlin for java_devs

129 views

Published on

Slides for my talk Kotlin for Java developers

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Kotlin for java_devs

  1. 1. Kotlin for Java developers: How and Why Marharyta Nedzelska
  2. 2. A few words about myself Software Engineer at Luxoft @JMargaritaN on Twitter https://www.facebook.com/jMargaritaN
  3. 3. Why did I learn Kotlin? Долгая и кровавая история
  4. 4. Why do we love Java - Cross-platform - No Pointers - Garbage collector - Backward compatibility - Multithreading - Comprehensive documentation - etc...
  5. 5. Why do we hate Java - Verbose - Too verbose - Even much more verbose - Backward compatibility - Slow development of language - NPE - Checked Exceptions
  6. 6. As a cosequence...
  7. 7. That’s why we have
  8. 8. A few words about Kotlin - Introduced by JetBrains - Was developed in 6 years - Static typing - Java interop - Compiles to bytecode - May be compiled to JS - Android development - Cool :)
  9. 9. Let’s start
  10. 10. Kotlin POJO var (+ setters) JAVA data (+ equals(), hashcode(), toString())
  11. 11. String templates Java
  12. 12. String templates Kotlin
  13. 13. Java - Explicit Casts
  14. 14. Kotlin - Smart Casts
  15. 15. When expression - when replaces switch - when matches its argument against all branches sequentially - when can be expression or statement - If when is used as expression then else branch is mandatory - we can use expressions (not only constants) as branch conditions - we can also check a value for being in or !in a range or a collection - if no argument is supplied, the branch conditions are simply boolean expressions
  16. 16. Stop it! Мне нельзя Kotlin! Я диабетик!
  17. 17. Kotlin - Null safety - Extension methods and properties - Delegates - Destructuring Declarations - Named and optional arguments - Operator overloading - Infix functions - etc...
  18. 18. Null safety
  19. 19. Null Safety - string : String -> string cannot be null - string : String? -> string can be null - Java types are named System type and can be String or String? either - ?., !!, ?: - help to deal with nullable
  20. 20. Let’s watch it in IDE
  21. 21. Puzzle!
  22. 22. What will be printed? A. NPE B. KotlinNPE C. “Kotlin is safe!” D. Won’t compile
  23. 23. How can we fix it?
  24. 24. Extension methods and properties - We can add new properties and methods to existing classes - Can be generic or for exact type - Are accessed as usual methods or properties - Are resolved statically - Cannot access to private functions and fields!
  25. 25. Let’s watch it in IDE
  26. 26. Delegation - We can inherit behavior using delegate pattern - We use keyword by - <Receiver>: <T> by <Delegate> : <Receiver>. f() -> <Delegate>.f() - <Delegate> is object of type <T>
  27. 27. Delegation
  28. 28. Delegated properties - Properties also can be delegated (their getters and setters) - We use same keyword by - <property> by <delegate> - <delegate> should have getValue() method for val - <delegate> should have getValue() and setValue() methods for var
  29. 29. Delegated properties
  30. 30. Destructuring declarations - Person must have component1(), component2() methods - In data classes componentN() methods are autogenerated
  31. 31. Named and optional arguments
  32. 32. Puzzle!
  33. 33. What will be printed? A. 1421 B. 1a42 C. Won’t compile D. IllegalArgumentException
  34. 34. How to fix it?
  35. 35. Infix functions - They are member functions or extension functions; - They have a single parameter;
  36. 36. Lambdas in Kotlin
  37. 37. Lambdas in Kotlin - Is always surrounded by { ...} - Its parameters (if any) are declared before -> (parameter types may be omitted) - it: implicit name of a single parameter - If lambda is a last argument, it can be written outside of the () - If the lambda parameter is unused, you can place an underscore instead of its name
  38. 38. Let’s watch it in IDE
  39. 39. Functions let, apply, when - <receiver>.let {//some code} - <receiver>.apply {//some code} - with(<receiver>) {//some code} this it returns let outer instance <receiver> anything apply <receiver> - <receiver> with <receiver> - anything
  40. 40. Let’s watch it in IDE
  41. 41. Puzzle!
  42. 42. What will be printed? A. 5.0 B. 5 C. () -> kotlin.Double D. Won’t compile
  43. 43. How to fix it? And 5.0 is printed!
  44. 44. Homework - Ranges - Lambdas (inline, noinline, crossinline) - Generics (+ reified) - Sealed classes, objects, enums - Operators overloading - Coroutines - Examples 18+ - Anything you wish
  45. 45. Kotlin Test
  46. 46. For testing we use - Java frameworks (JUnit, TestNG…) - KotlinTest - Spek - Mockito-Kotlin
  47. 47. KotlinTest - String Spec - Fun Spec - Should Spec - Word Spec - Feature Spec - Behavior Spec - Free Spec
  48. 48. Matchers - obj shouldBe other - shouldThrow<E> { block } - map should haveKey("key") - str should match(regex) - int shouldBe between(x, y) - double shouldBe (value plusOrMinus(tolerance)) - collection should contain(element) - Full list of Matchers: https://github.com/kotlintest/kotlintest/blob/master/doc/matchers.md
  49. 49. Property-based testing Let the Computer Generate Your Test Data
  50. 50. Table Testing
  51. 51. Interceptors
  52. 52. Test asynchronous code
  53. 53. Tune test execution
  54. 54. How did I learn Kotlin - kotlinlang.org - Kotlin Koans - Conference talks - Book “Kotlin in Action” The best way to learn Kotlin is Code Kotlin!
  55. 55. The end...

×