Introduction to Programming in Kotlin

Kotlin is a modern, statically typed programming language developed by JetBrains. It is fully interoperable with Java and is the preferred language for Android development. Kotlin combines object-oriented and functional programming features, offering concise syntax, null safety, and powerful tools for building robust applications.

This guide will introduce you to the fundamentals of Kotlin programming, helping you write clean, expressive, and efficient code.


Table of Contents

  1. What is Kotlin?
  2. Setting Up Your Environment
  3. Variables and Data Types
  4. Control Flow
  5. Functions
  6. Null Safety
  7. Collections
  8. Object-Oriented Programming
  9. Functional Programming
  10. Coroutines

What is Kotlin?

Kotlin is a statically typed, general-purpose programming language designed to be concise, expressive, and interoperable with Java. It is widely used for Android development, server-side applications, and more.

// Example: Your first Kotlin program
fun main() {
    println("Hello, Kotlin Programming!")
}

Setting Up Your Environment

To write and run Kotlin programs, you need the Kotlin compiler or an IDE like IntelliJ IDEA. You can also use the online Kotlin Playground.

# Example: Running a Kotlin program
kotlinc main.kt -include-runtime -d main.jar
java -jar main.jar

Variables and Data Types

Kotlin is statically typed, but it supports type inference, allowing you to omit explicit type declarations. Common data types include Int, Double, String, and Boolean.

// Example: Declaring variables
fun main() {
    val age: Int = 25 // Immutable variable
    var height = 5.9 // Mutable variable with type inference
    val name = "Alice"
    val isStudent = true

    println("Name: $name, Age: $age, Height: $height")
}

Control Flow

Kotlin supports if, when, and loops (for, while, do-while) for controlling program flow.

// Example: Conditional statement
fun main() {
    val age = 18

    if (age >= 18) {
        println("You are an adult.")
    } else {
        println("You are a minor.")
    }
}

Functions

Functions are defined using the fun keyword. Kotlin supports default arguments, named arguments, and single-expression functions.

// Example: Function
fun add(a: Int, b: Int): Int {
    return a + b
}

fun main() {
    val result = add(5, 10)
    println("Result: $result") // 15
}

Null Safety

Kotlin’s type system distinguishes between nullable and non-nullable types, helping to avoid null pointer exceptions.

// Example: Null safety
fun main() {
    val name: String? = null // Nullable type
    val length = name?.length ?: 0 // Safe call operator with Elvis operator

    println("Length: $length") // 0
}

Collections

Kotlin provides a rich set of collections, including lists, sets, and maps, with functional operations like map, filter, and reduce.

// Example: List
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val evenNumbers = numbers.filter { it % 2 == 0 }

    println("Even Numbers: $evenNumbers") // [2, 4]
}

Object-Oriented Programming

Kotlin supports classes, inheritance, interfaces, and data classes for object-oriented programming.

// Example: Data class
data class Person(val name: String, val age: Int)

fun main() {
    val person = Person("Alice", 25)
    println("Name: ${person.name}, Age: ${person.age}")
}

Functional Programming

Kotlin supports functional programming features like higher-order functions, lambdas, and immutability.

// Example: Higher-order function
fun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

fun main() {
    val sum = operateOnNumbers(5, 10) { x, y -> x + y }
    println("Sum: $sum") // 15
}

Coroutines

Kotlin’s coroutines provide a way to write asynchronous, non-blocking code in a sequential style.

// Example: Coroutine
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(1000L) // Non-blocking delay
        println("World!")
    }
    println("Hello,")
}