Photo by Hunter Haley on Unsplash

Hello, Kotlin!

What is Kotlin?

How to start?

Variables


val age = 56 // immutable variable
age = 120 // Compiler error: val cannot be reassigned
var protocol = “ssh” // mutable variable
protocol = “https” // OK

String templates

val name = “Tom”
val age = 56
println(“User: $name with age: $age has entered details”) // prints “User: Tom with age: 56 has entered details”

Type inference

val name = “Tom” // type inferred to string
val money: Double = 123.4 // type declared explicitly

Functions

fun assignWorker(fabric: String, supervisor: String, totalWorkingHours: Int = 40) {
// …
}
assignWorker(“Denim”, “Supervisor”) // totalWorkingHours is 40
assignWorker(fabric = “Denim”, supervisor = “Supervisor”) // same as above
assignWorker(supervisor = “Supervisor”, fabric = “Denim”) // same as above
assignedWorker("Denim", "Supervisor", 10) // totalWorkingHours is 10

Classes and properties

class Car(val type: String, var isMoving: Bool)// usage
var bmw = Car(“BMW”, false)
bmw.isMoving = true
class Frame(val height: Int, val width: Int) {
val area: Int
get() = {
return height * width
}
}
// usage
val frame = Frame(2, 2)
println(frame.area) // prints “4”

Simple POJOs

data class Boat(val loadCapacity: Int, val type: String)

When expression

when (number) {
1 -> println(“only one”)
2 -> println(“a pair”)
12 -> println(“a dozen”)
101..999 -> println(“more than a hundred, but less then thousand”)
}
enum class Color {
RED, YELLOW, BLUE
}
when (setOf(color1, color2)) { // switch on set
setOf(RED, YELLOW) -> println(“orange”)
setOf(RED, BLUE) -> println(“purple”)
setOf(YELLOW, BLUE) -> println(“green”)
}
func countryByType(car1: Car, car2: Car) = {
when {
(car1.type == “BMW” && car2.type == “Audi”) ||
(car1.type == “Audi” && car2.type == “BMW”) -> “Germany”
(car1.type == “Renault” && car2.type == “Peugeout”) ||
(car1.type == “Peugeout” && car2.type == “Renault”) -> “France”
(car1.type == “Ferrari” && car2.type == “Lamborghini”) ||
(car1.type == “Lamborghini” && car2.type == “Ferrari”) -> “Italy”
else -> throw Exception(“Unknown type”)
}
}

Smart casts

if (obj is Car) {
obj.move()
}

Nullability

var name = “Tom”
name = null // compiler error
var secretName: String? = “Agent X”
secretName = null // OK
val length = secretName?.length // returns null, as secretName is null
val defaultLength = secretName?.length ?: 0 // returns zero
val error = secretName!!.length // throws NullPointerException

Ranges and progressions

val oneToHundred = 1..100for (i in 1..100) { … }
for (i in 1 until 100) { … } // same as above
for (i in oneToHundred) { … } // same as above
for (i in 100 downTo 1) { … } // reversefor (i in 10..100 step 10) { … } // only tens

Statically initialize collections

val wheels = listOf(2, 4, 6, 8, 12)
val cars = setOf(“BMW”, “Renault”, “Peugeout”, “Lamborghini”)
val agentNames = mapOf(1 to “The Boss”, 2 to “Left Hand”, 5 to “Agent X”)
for ((rank, name) in agentNames) {
println(“Agent ${name} ranks as #${rank}”)
}

Extension functions and properties

fun String.lastChar(): Char = this.get(this.length — 1)fun String.prefix(val value: String): String {
return value + this
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store