I'm learning Kotlin, so I have been updating it with examples and explanations about the language that I'm using at work.
Examples of projects using Kotlin:
- .ktsKotlin script
- .ktKotlin class
There are two ways to declare variables using Kotlin: val and var.
- valis a constant
- varfor a variable whose value can change
Example: 01-variable.kts
It's possible use conditional statements to assign a value to variable:
val answerString: String = if (count == 42) {
    "I have the answer."
} else if (count > 35) {
    "The answer is close."
} else {
    "The answer eludes me."
}We must pass params with types and every function needs return type too.
fun sum(a: Int, b: Int): Int {
    return a + b
}
// or
fun sum(a: Int, b: Int) = a + bExample: 02-functions.kts
There are some ways to concatenate variables in a String
fun bestAnime(): String {
    return "Naruto"
}
print("Best Animes:" + bestAnime())
print("Best Animes: ${bestAnime()}")Example of Try-catch block for exception handling
try {
    val message = "Hello World! I love Digimon!"
    message.toInt()
} catch (exception: NumberFormatException) {
    // ...
}Instead to use getters and setters we can use properties
- Getter
class Pokemon(val name: String, val type: String) {
    val description: String
        get() = "Pokemon ${name} is ${type} type"
}- Setter
var power = 0.0
    set(value) {
        if (value > 0) {
            field = value
        }
    }Example: 03-properties.kts
enum class Console {
    GBA,
    PLAYSTATION,
    XBOX,
    SWITCH,
    PC
}Example: 04-enums.kts
Creating a class using Enum
class Game(val name: String,
           val console: Console) {
    fun play(): String {
        return "You're playing $name on $console"
    }
}Example: 05-classes.kt
There is some ways to create lists in Kotlin
var digimons = mutableListOf("Agumon", "Tailmon", "Angemon")
var digimons = listOf("Agumon", "Tailmon", "Angemon")
val digimonPower: MutableMap<Int, Int> = mutableMapOf(0 to 50, 1 to 50, 2 to 100)
// Empty List
var pokemons = arrayListOf<String>()When is similar Java Switch
fun getPrice(console: Console): Int =
    when (console) {
        Console.GBA -> 1000
        Console.PLAYSTATION -> 5299
        Console.SWITCH -> 2500
        Console.XBOX -> 4200
        Console.PC -> 5000
    }Example: 06-when.kt
Example of how to iterate over a map
val binaryRepresentation = TreeMap<Char, String>()
for (c in 'A'..'F') {
    val binary = Integer.toBinaryString(c.toInt())
    binaryRepresentation[c] = binary
}
for ((letter, binary) in binaryRepresentation) {
    println("$letter - $binary")
}Example: 08-interatingovermap.kt
It's possible to use "in" in a conditional
fun isLetter(c: Char) = c in 'a'..'z' || c in 'A'..'Z'
fun isNotDigit(c: Char) = c !in '0'..'9'Example: 09-inoperation.kt
We can use Mockito, but the best lib to mock using kolint is Mockk
val car = mockk<Car>()
every { car.drive(Direction.NORTH) } returns Outcome.OK
car.drive(Direction.NORTH) // returns OK
verify { car.drive(Direction.NORTH) }
confirmVerified(car)- Learn the Kotlin programming language
- Get started with Kotlin - KotlinLang.org
- Curso de Kotlin 2020 | Básico
developed by Jean Jacques Barros
