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

Java vs Kotlin

Java vs Kotlin

Kotlin is bypassing all other JVM languages with incredible pace. In Android development, it is already the language to use! But Java also evolved in meantime. Let’s look at Kotlin and compare its features side by side.

Danny Preussler

June 04, 2019
Tweet

More Decks by Danny Preussler

Other Decks in Programming

Transcript

  1. @PreusslerBerlin about:me • Java developer since 2003 • Kotlin developer

    since 2016 • Google Developer Expert for Android and Kotlin • Introduced Kotlin at Groupon and Viacom
  2. @PreusslerBerlin sporttotal.tv • We are streaming under- medialised sports •

    Amateur and semi professional football • Professional Volleyball…
  3. @PreusslerBerlin Kotlin adoption • Languages developers want to learn in

    2019 • https://research.hackerrank.co m/developer-skills/2019
  4. @PreusslerBerlin Kotlin adoption Okt 18, GitHub: 'Kotlin for Android now

    fastest-growing programming language' https://www.zdnet.com/article/microsofts-github-kotlin-for-android- now-fastest-growing-programming-language/
  5. @PreusslerBerlin Kotlin adoption • JVM Ecosystem Report 2018 (10k questionaires):

    What is the principal JVM language you use for your main applications? • Kotlin edges past Groovy and Scala in language usage on 2.42% https://snyk.io/blog/jvm-ecosystem-report-2018/
  6. @PreusslerBerlin Kotlin adoption • 1.5M+ developers edited Kotlin code in

    2018 • 100M+ lines of Kotlin code • 96,000+ Kotlin GitHub repositories • Android — 1 in 4 apps (from top 1000) uses Kotlin • Server — х2 from 2017 https://www.jetbrains.com/annualreport/2018/community/
  7. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 The year: 2010 Java6 has been around the 4th year
  8. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 The year: 2010 Kotlin Development started internally
  9. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 July 2010 Swift Development started
  10. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 July 2011 JetBrains unveiled Project Kotlin
  11. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 February 2012 JetBrains open sourced Kotlin
  12. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 June 2014 WWDC application became the first publicly released Swift app
  13. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 Google I/O 2017 Official Kotlin support for Android
  14. @PreusslerBerlin 2010 2011 2012 2013 2014 2015 2016 2017 2018

    2019 Google I/O 2019 Kotlin becomes preferred language for Android
  15. @PreusslerBerlin Goals of Kotlin Modern language Faster compilation than Scala

    Smaller footprint than Scala Designed to work perfectly with Java (designed for 6+8)
  16. @PreusslerBerlin Type inference in Java 10 Difference to Kotlin: •

    var is not a keyword in Java 10 • it‘s a reserved type name
  17. @PreusslerBerlin Lets talk null I call it my billion-dollar mistake.

    It was the invention of the null reference in 1965 Tony Hoare ( inventor of the null reference)
  18. @PreusslerBerlin Lets talk null • Equivalent in Java: @NonNull but

    runtime crash vs compiler only • Nullability is Part of Typesystem
  19. @PreusslerBerlin Lets talk null fun some(thing: String) {…} public final

    void some( @NotNull String thing) { checkParameterIsNotNull(thing, "thing"); }
  20. @PreusslerBerlin default parameters vs overloading class Api { Api(String baseURl)

    { this(baseURl, CacheStrategy.NO_CACHE) } Api(String baseURl, CacheStrategy cache) { } }
  21. @PreusslerBerlin default parameters vs overloading class Api( private val baseUrl:

    String, val cache: CacheStrategy = CacheStrategy.NO_CACHE )
  22. @PreusslerBerlin Sealed classes sealed class LoadingState { class Loading :

    LoadingState() class Loaded : LoadingState() class Empty : LoadingState() }
  23. @PreusslerBerlin Lazy initialization public class LazyField { private String value;

    public String getValue() { if (value == null) { value = computeValue(); } return value } private String computeValue() { .... } }
  24. @PreusslerBerlin Lazy initialization class LazyField { val: String by lazy

    { computeValue() } private fun computeValue()= … }
  25. @PreusslerBerlin Lazy initialization • Framework class like Activities: no control

    over constructor • How use the compiler to check for null?
  26. @PreusslerBerlin Late initialization • Framework class like Activities: no control

    over constructor • How use the compiler to check for null?
  27. @PreusslerBerlin Primitives in Kotlin • No primitives • Boxing? Therefore

    no boxing issues (mostly) int a; Integer b; a: Integer
  28. @PreusslerBerlin Primitives in Kotlin: Costs? • The numeric type usages

    in Kotlin are compiled into JVM primitives where possible. • Compiler decides
  29. @PreusslerBerlin Primitives in Kotlin: Costs? • The numeric type usages

    in Kotlin are compiled into JVM primitives where possible. • Compiler decides • Works because of missing widening int a = 1; double b = a; val a:Int = 1 val b:Double = a
  30. @PreusslerBerlin Primitives in Kotlin • Nullable type cannot be primitive

    • Primitives cannot be used as a generic type argument. • List<Int> is equivalent of Java List<Integer> • Therefore IntArray as type!
  31. @PreusslerBerlin Operator overloading operator fun plusAssign(callback: Callback) { callbacks.add(callback) }

    operator fun minusAssign(callback: Callback) { callbacks.remove(callback) } adapterCallbacks += {…}
  32. @PreusslerBerlin Expression Translated to a++ a.inc() + see below a--

    a.dec() + see below Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.not() Expression Translated to a + b a.plus(b) a - b a.minus(b) a * b a.times(b) a / b a.div(b) a % b a.rem(b), a.mod(b) (deprecated) a..b a.rangeTo(b)
  33. @PreusslerBerlin Operator overloading Expression Translated to a > b a.compareTo(b)

    > 0 a < b a.compareTo(b) < 0 a >= b a.compareTo(b) >= 0 a <= b a.compareTo(b) <= 0 Expression Translated to a == b a?.equals(b) ?: (b === null) a != b !(a?.equals(b) ?: (b === null))
  34. @PreusslerBerlin Operator overloading Expression Translated to a in b b.contains(a)

    a !in b !b.contains(a) Expression Translated to a[i] a.get(i) a[i, j] a.get(i, j) a[i_1, ..., i_n] a.get(i_1, ..., i_n) a[i] = b a.set(i, b) a[i, j] = b a.set(i, j, b) a[i_1, ..., i_n] = b a.set(i_1, ..., i_n, b)
  35. @PreusslerBerlin Operator overloading Expression Translated to a() a.invoke() a(i) a.invoke(i)

    a(i, j) a.invoke(i, j) a(i_1, ..., i_n) a.invoke(i_1, ..., i_n) Expression Translated to a += b a.plusAssign(b) a -= b a.minusAssign(b) a *= b a.timesAssign(b) a /= b a.divAssign(b) a %= b a.remAssign(b), a.modAssign(b) (deprecated)
  36. @PreusslerBerlin Operator overloading • Range expression for (i in 1..4)

    print(i) operator fun rangeTo(other: Int) = IntRange(this, other)
  37. @PreusslerBerlin Getter / Setter val live get() = category.live public

    final boolean getLive() { return category.getLive(); }
  38. @PreusslerBerlin Delegation • inheritance Effective Java: Item 16: Favor composition

    over inheritance class FirebaseFeatureFlags ( private val defaults: DefaultFeatureFlags ) : FeatureFlags by defaults {
  39. @PreusslerBerlin inline •Powerful with reified: Allows generics with type check:

    T::class inline fun <reified T : Context> Activity.start() { startActivity(Intent(this, T::class.java)) }
  40. inline inline fun <reified T : Context> Activity.start() { startActivity(Intent(this,

    T::class.java)) } start<MainActivity>() $receiver$iv.startActivity( new Intent( (Context)$receiver$iv, MainActivity.class) );
  41. @PreusslerBerlin inline • Inline classes • Experimental • Inline classes

    are final • Can lead to weird boxing issues • Not compatible with @Parcelize inline class Time(private val long timeMs)
  42. @PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods

    final by default (vs open) • Inner class: static by default (vs inner)
  43. @PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods

    final by default (vs open) • Inner class: static by default (vs inner) • Stricter usage of protected
  44. @PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods

    final by default (vs open) • Inner class: static by default (vs inner) • Stricter usage of protected • New visibility: internal
  45. @PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods

    final by default (vs open) • Inner class: static by default (vs inner) • Stricter usage of protected • New visibility: internal • One file can have multiple public classes
  46. @PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods

    final by default (vs open) • Inner class: static by default (vs inner) • Stricter usage of protected • New visibility: internal • One file can have multiple public classes • „global“ functions & variables are allowed
  47. @PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods

    final by default (vs open) • Inner class: static by default (vs inner) • Stricter usage of protected • New visibility: internal • One file can have multiple public classes • „global“ functions & variables are allowed • No checked Exceptions
  48. @PreusslerBerlin Java Interop public final class FileKt { public static

    final void foobar() {..} } //File.kt fun foobar(){..}
  49. @PreusslerBerlin Java Interop @Metadata( mv = {1, 1, 13}, bv

    = {1, 0, 3}, k = 1, d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010 \u0000\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u00 02\b\u0003\u0018\u00002\u00020\u0001:\u0001\u000 6B\u0005¢\u0006\u0002\u0010\u0002J\b\u0010\u0003 \u001a\u00020\u0004H\u0007J\u0006\u0010\u0005\u0 01a\u00020\u0004¨\u0006\u0007"}, d2 = {"Lde/jodamob/android/kotlin/T_TestingTest;", "", "()V", "test", "", "throwSomething", "Something", "app"} )
  50. @PreusslerBerlin Java Interop • Performance in most case same as

    Java https://de.slideshare.net/intelliyole/kotlin-bytecode-generation-and-runtime-performance
  51. @PreusslerBerlin Less Code Kotlin .. helps to increase a cut

    in the lines of code by approximately 40 % (JetBrains) … tests show … the number of code lines reduces by 30… https://belitsoft.com/apps-development-services/java-vs-kotlin
  52. @PreusslerBerlin Kotlin ‘Kotlin isn't revolutionary (with the possible exception of

    its null-handling) but feels like a very careful amalgamation of some of the best features of other languages. Rob Fletcher, Netflix https://belitsoft.com/apps-development-services/java-vs-kotlin