Getting started with Kotlin

Serban Iordache

What is Kotlin?

"Kotlin is the best programming language in the universe."
A friend of mine

Who should learn Kotlin?

Everybody should learn Kotlin!

  • You should learn Kotlin
  • Your friends should learn Kotlin
  • Your cat should learn Kotlin

    • the most successful cats in the world are proficient in Kotlin
    • bring your cat to a Kotlin training center today!

Let’s start learning Kotlin!

Hello World

package org.beryx.talk

fun main(args: Array<String>) {
    println("Hello, world!")
}
  • this program prints Hello, world!
  • the package statement appears at the top of the source file
  • program arguments are passed as an array of strings

Hello World (with arguments)

design
fun main(args: Array<String>) {
    val name = if(args.size > 0) args[0] else "world"
    println("Hello, $name!")
}
  • if no argument is passed, the program prints Hello, world!
  • otherwise, the value of the first argument is used in the greeting

Integer arithmetic operators

Binary operators
Expression Meaning

a + b

addition

a - b

subtraction

a * b

multiplication

a / b

division

a % b

remainder

Integer arithmetic operators

val x = 6
val y = 2
println("x + y = ${x + y}")  // Addition: x + y = 8

Integer arithmetic operators

val x = 6
val y = 2
println("x + y = ${x + y}")  // Addition: x + y = 8
println("x - y = ${x - y}")  // Subtraction: x - y = 4

Integer arithmetic operators

val x = 6
val y = 2
println("x + y = ${x + y}")  // Addition: x + y = 8
println("x - y = ${x - y}")  // Subtraction: x - y = 4
println("x * y = ${x * y}")  // Multiplication x * y = 12

Integer arithmetic operators

val x = 6
val y = 2
println("x + y = ${x + y}")  // Addition: x + y = 8
println("x - y = ${x - y}")  // Subtraction: x - y = 4
println("x * y = ${x * y}")  // Multiplication x * y = 12
println("x / y = ${x / y}")  // Division: x / y = 3

Integer arithmetic operators

val x = 6
val y = 2
println("x + y = ${x + y}")  // Addition: x + y = 8
println("x - y = ${x - y}")  // Subtraction: x - y = 4
println("x * y = ${x * y}")  // Multiplication x * y = 12
println("x / y = ${x / y}")  // Division: x / y = 3
println("x % y = ${x % y}")  // Remainder: x % y = 0