about:me •Java developer since 2003 •Kotlin developer since 2016 •Google Developer Expert for Android and Kotlin •Introduced Kotlin at Groupon and Viacom
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/
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
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!
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)
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))
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)
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)
inline •Inline classes •Experimental •Inline classes are final •Can lead to weird boxing issues •Not compatible with @Parcelize inline class Time(private val timeMs: Long)
● Lightweight threads ● Allows asynchronous code to be written sequentially suspend fun concurrentSum(): Int = coroutineScope { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } one.await() + two.await() }
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
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
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
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
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
Runtime ● Execution time: difference [..] can vary from 6.7% in favor of Java [..], to 1.2% in favor of Kotlin [..] ● Memory consumption: appeared the lowest in the Java implementation in four out of six benchmarks. ● CPU load: no significant distinction in load management https://medium.com/@bards95/comparative-evaluation-of-selected-constru cts-in-java-and-kotlin-part-1-dynamic-metrics-2592820ce80
Runtime ● Execution time: difference [..] can vary from 6.7% in favor of Java [..], to 1.2% in favor of Kotlin [..] ● Memory consumption: appeared the lowest in the Java implementation in four out of six benchmarks. ● CPU load: no significant distinction in load management https://medium.com/@bards95/comparative-evaluation-of-selected-constru cts-in-java-and-kotlin-part-1-dynamic-metrics-2592820ce80
Runtime ● Execution time: difference [..] can vary from 6.7% in favor of Java [..], to 1.2% in favor of Kotlin [..] ● Memory consumption: appeared the lowest in the Java implementation in four out of six benchmarks. ● CPU load: no significant distinction in load management https://medium.com/@bards95/comparative-evaluation-of-selected-constru cts-in-java-and-kotlin-part-1-dynamic-metrics-2592820ce80
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
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