Introduction to Kotlin
Note this blog post is a summary of my notes from the introductory chapter of the book Atomic Kotlin. Consider buying a copy of the book for a more detailed reference.
In Kotlin, every data type is an object. You can store either immutable or mutable references to objects.
val references are immutable while var references are mutable.
To store an object reference to an identifier use an assignment statement:
var identifier: Type = initialization
Identifiers must start with an _ or a letter. Identifiers can contain _, letters, or numbers. By convention identifiers start with lowercase letters.
Data types are capitalized by convention.
Data types include:
- Int
- Long
- Double
- Boolean
- Char
- String
Functions
Functions are the most basic way to reuse code in Kotlin. Function signatures consist of the name, parameters, and return type. Functions can have block or expression bodies. Expression body functions must contain a single expression from which Kotlin can infer the return type.
Basic product function definition with a block and expression body
// Block body
fun product(a: Int, b: Int): Int {
return a * b
}
// Expression body
fun product(a: Int, b: Int) = a * b
String Templates
Kotlin has an easy way to insert identifiers into Strings using the concept of string templates. Just prefix an identifier with a $ and Kotlin does the rest.
fun main() {
val x = 5
println("X is $x")
}
You can optionally include an expression in a string template by using braces.
fun main() {
val x = 5
println("X is ${x + 1}")
}
If Expressions
Expressions in Kotlin produce an effect. In Kotlin, conditional logic is considered an expression. If expressions can contain an expression or block body. Because conditionals are expressions you can assign them to an identifier.
fun main() {
if (1 > 0)
println("It's true")
}
These are the common logical operators in Kotlin:
- >
- <
- >=
- <=
- ==
- !=
- !
- &&
- ||
You can store a logical expression inside a variable like so:
fun main() {
val x = 1 > 0
if (x)
println("It's true")
}
More on Numerical Types and Operators
Standard numerical operators include:
- +
- -
- *
- /
- %
- +=
- -=
- *=
- /=
- %=
There are also prefix and postfix increment and decrement operators:
- ++
- –
You can access the min and max values of the Int and Long data types like so:
fun main() {
val x = Int.MAX_VALUE
println(x)
}
Developers are responsible for handling overflow. Overflow does not crash programs.
Loops in Kotlin
Kotlin supports the following types of loops:
- while
- do-while
- for
- repeat
The basic syntax for the loops:
fun main() {
while (Boolean-expression) {
// Code to be repeated
}
}
fun main() {
do {
// Code to be repeated
} while (Boolean-expression)
}
fun main() {
for (i in 0..3) {
println(i)
}
}
You can define ranges in different ways in Kotlin:
- 1..10
- 0 until 10
- 5 downTo 1
- 0..9 step 2
- 0 until 10 step 3
- 9 downTo 2 step 3
You can assign ranges to identifiers. The data type for many common ranges is IntProgression.
Ranges can be defined between a set of characters and ranges.