Table of Contents
Motivation
I wanted to try writing an Android app. In spite of using plain old Java I wanted to give the new kid on the block Kotlin (from the Jetbrains folks) a try.
First observations
Coming from a Python background it’s good to see that Kotlin doesn’t use semi-colons! But the curly braces from Java make code blocks are still there, hmpf…
Variables
Kotlin has two ways of declaring “variables”:
- var: think variable: this is a mutable
- val: think values: this is read-only like const
Data Types and Type Inference
A nice thing about Kotlin is type inference. When it is clear which data type is meant you can leave out the data type from the declaration
Strings
val firstName = "Tony" val lastName = "Stark"
Concatenation
You can concatenate strings with the plus operator as you can do it in Python
val fullName: String = firstName + " " + lastName
Print and String interpolation
Kotlin has a built-in println function so you can stop typing System.out.println()
Less visual noise!
println(fullName)
A neat little feature is the string interpolation syntax. If You want to concatenate strings for logging output, you can write $stringName Locks similar to the Python f-Strings
println("Name: $fullName")
But there is more to it:
You can for example call functions inside strings as well:
println("String length: ${fullName.length}")
or access indices
println("2nd Index: ${fullName[2]}")