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.
@PreusslerBerlin about:me • Java developer since 2003 • Kotlin developer since 2016 • Google Developer Expert for Android and Kotlin • Introduced Kotlin at Groupon and Viacom
@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/
@PreusslerBerlin Goals of Kotlin Modern language Faster compilation than Scala Smaller footprint than Scala Designed to work perfectly with Java (designed for 6+8)
@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)
@PreusslerBerlin Sealed classes sealed class LoadingState { class Loading : LoadingState() class Loaded : LoadingState() class Empty : LoadingState() }
@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
@PreusslerBerlin Primitives in Kotlin • Nullable type cannot be primitive • Primitives cannot be used as a generic type argument. • List is equivalent of Java List • Therefore IntArray as type!
@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)
@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))
@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)
@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)
@PreusslerBerlin inline •Powerful with reified: Allows generics with type check: T::class inline fun Activity.start() { startActivity(Intent(this, T::class.java)) }
@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)
@PreusslerBerlin Other changes • Classes/methods public by default • Classes/methods final by default (vs open) • Inner class: static by default (vs inner)
@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
@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
@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
@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
@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
@PreusslerBerlin Java Interop • Performance in most case same as Java https://de.slideshare.net/intelliyole/kotlin-bytecode-generation-and-runtime-performance
@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
@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