Part 0 · 1 chapters · ~8 min
Basics
Installing Kotlin and running code (kotlinc, Gradle, IntelliJ), val and var, type inference, functions and expression bodies, string templates, if and when as expressions, data classes, collections and lambdas, extension functions, and a first program.
1
First code
code
data class Transfer(val id: String, val amountKobo: Long, val channel: Channel, val status: Status)
enum class Channel { APP, USSD }
enum class Status { PENDING, COMPLETED, FAILED }
fun fee(amountKobo: Long, channel: Channel): Long = when {
channel == Channel.USSD -> 2_000
amountKobo <= 500_000 -> 1_000
amountKobo <= 5_000_000 -> 2_500
else -> 5_000
}
fun Long.toNaira(): String = "₦%,d.%02d".format(this / 100, this % 100) // extension function
val total = transfers.filter { it.status == Status.COMPLETED }.sumOf { it.amountKobo + fee(it.amountKobo, it.channel) }
println(total.toNaira())
// kotlinc-jvm App.kt -include-runtime -d app.jar && java -jar app.jar (Kotlin 2.4.20 on JVM 26 here)KOTLIN IN SIX IDEAS
what it changes from Java
swipe the figure sideways, or tap expand for full screen
1/4
concise
val, type inference and expression-bodied functions remove much of Java's boilerplate while compiling to the same bytecode.
less ceremonysame JVM