DEV Community

Cover image for Kotlin Note
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Kotlin Note

Kotlin Beginners

Every kotlin starts with fun main

fun main() {
    println("Hello World")
}
Enter fullscreen mode Exit fullscreen mode

Variables

Declarations and Assignments

fun main() {
    var age:Int // declaration
    age = 20 // assignment

    val name:String = "John" // declaration and assignment
}
Enter fullscreen mode Exit fullscreen mode

Data types

fun main() {
    val number: Int = 89
    val bool: Boolean = true
    val floatNumber: Float = 5.9F
    val double: Double = 67.90
    val longNumber: Long = 9000000000000000L
    val char: Char = 'A'
    val string: String = "Hello Kotlin"
    val byteNumber: Byte = 127
    val shortNumber: Short = 32000
    val nullableInt: Int? = null

    println("Int: $number")
    println("Boolean: $bool")
    println("Float: $floatNumber")
    println("Double: $double")
    println("Long: $longNumber")
    println("Char: $char")
    println("String: $string")
    println("Byte: $byteNumber")
    println("Short: $shortNumber")
    println("Nullable Int: $nullableInt")

    // string methods
    println(string.length)
    println(string.toUpperCase())
    println(string.toLowerCase())
    println(string.contains("Kotlin"))
    println(string.startsWith("Hello"))
    println(string.endsWith("Kotlin"))
    println(string.replace("Kotlin", "World"))
    println(string.substring(0, 5))
    println(string.split(" "))
}
Enter fullscreen mode Exit fullscreen mode

Return types

fun main() {
    printHello()
}

fun printHello(): Unit { // Unit is optional for void return types
    println("Hello")
}

// others return types are Int, String, Boolean, etc.
Enter fullscreen mode Exit fullscreen mode

Short hand for one line functions

fun main() {
    println(add(1, 2))
}

fun add(a: Int, b: Int) = a + b
Enter fullscreen mode Exit fullscreen mode

Readline

fun main() {
    try {
        println("Enter number one: ")
        val numberOne: String = readlnOrNull() ?: "0"
        println("Enter number two: ")
        val numberTwo: String = readlnOrNull() ?: "0"

        // Safely convert to Int
        val result = numberOne.toIntOrNull() ?: 0 + numberTwo.toIntOrNull() ?: 0
        println("The result is: $result")
    } catch (e: Exception) {
        println("Error: Invalid input. Please enter numeric values.")
    }
}

Enter fullscreen mode Exit fullscreen mode

listof (Immutable) i.e can't add or remove items

fun main() {

   val animals:List<String> = listOf<String>("dog","lion", "tiger") // immutable list
    println(animals)
    println(animals[0])
    println(animals.size) //3

    for (a in animals){
            println("I am a $a")
    }
}
Enter fullscreen mode Exit fullscreen mode

mutable list

fun main() {

    val animals: MutableList<String> = mutableListOf("dog", "lion", "tiger") // immutable list
    animals.add("fish")
    animals.add("turkey")
    animals.removeFirst()
    animals.removeLast()
    animals.removeAt(2)
    for (a in animals) {
        println("I am a $a")
    }
}
Enter fullscreen mode Exit fullscreen mode

set

fun main() {
    val animals = setOf<String>("dog","lion","lion", "tiger") // immutable list that doesn't allow duplicates
    println(animals)

    for (a in animals) {
        println("I am a $a")
    }
}
Enter fullscreen mode Exit fullscreen mode

map

fun main() {
    val animals = mapOf<String, String>("dog" to "bark", "lion" to "roar", "tiger" to "roar") // immutable list that doesn't allow duplicates
    println(animals)

    for (a in animals) {
        println("I am a ${a.key} and I ${a.value}")
    }

     val husbandWife = mapOf("mimi" to "yomi", "dele" to "deola", "mummy" to "daddy")
    println(husbandWife) //{mimi=yomi, dele=deola, mummy=daddy}
    println(husbandWife.keys) // [mimi, dele, mummy]
    println(husbandWife.values) // [yomi, deola, daddy]
    println(husbandWife.entries) // [mimi=yomi, dele=deola, mummy=daddy]
}
Enter fullscreen mode Exit fullscreen mode

Loops

fun main() {
    val animals = listOf<String>("dog","lion","lion", "tiger") // immutable list that doesn't allow duplicates
    println(animals)

    for (a in animals) {
        println("I am a $a")
    }

    for (i in 0..10) {
        println(i)
    } // 0 1 2 3 4 5 6 7 8 9 10

    for (i in 0 until 10) {
        println(i)
    } // 0 1 2 3 4 5 6 7 8 9

    for (i in 10 downTo 0) {
        println(i)
    } // 10 9 8 7 6 5 4 3 2 1 0

    for (i in 0..10 step 2) {
        println(i)
    } // 0 2 4 6 8 10

    for (i in 10 downTo 0 step 2) {
        println(i)
    } // 10 8 6 4 2 0

//while loop
    var i = 0
    while (i < 10) {
        println(i)
        i++
    }
}
Enter fullscreen mode Exit fullscreen mode

ForEach

fun main() {
    val animals = listOf<String>("dog","lion","lion", "tiger") // immutable list that doesn't allow duplicates
    println(animals)

    animals.forEach { animal ->
        println("I am a $animal")
    }

    animals.forEachIndexed { index, animal ->
        println("I am a $animal at index $index")
    }
}
Enter fullscreen mode Exit fullscreen mode

Functions

fun main() {
    println("Hello World")
    println(add(2, 3, 4)) // 9

}


fun add(a: Int, b: Int, c: Int): Int {
    return a + b + c
}

Enter fullscreen mode Exit fullscreen mode

Function Overloading

fun main() {
    println(add(2, 3)) // 5
    println(add(2, 3, 4)) // 9
}

// same name with different parameters
fun add(a: Int, b: Int): Int {
    return a + b
}

fun add(a: Int, b: Int, c: Int): Int {
    return a + b + c
}
Enter fullscreen mode Exit fullscreen mode

Extension Functions

fun main() {
    val list = listOf<String>("dog", "cat", "lion")
    list.printList()
}

fun List<String>.printList() {
    for (item in this) {
        println(item)
    }
}
Enter fullscreen mode Exit fullscreen mode
fun Int.isEven(): Boolean {
    return this % 2 == 0
}

fun main() {
    val number = 4
    println("$number is even: ${number.isEven()}") // Output: 4 is even: true
}
Enter fullscreen mode Exit fullscreen mode

Named and Default Parameters

fun greet(name: String, greeting: String = "Hello") {
    println("$greeting, $name!")
}

fun main() {
    greet("John")           // Uses the default value for 'greeting'
    greet("Jane", "Hi")     // Overrides the default value
}
Enter fullscreen mode Exit fullscreen mode
fun orderPizza(size: String, crust: String = "Thin", toppings: String = "Cheese") {
    println("Size: $size, Crust: $crust, Toppings: $toppings")
}

fun main() {
    orderPizza(size = "Large")                      // Uses defaults for crust and toppings
    orderPizza(size = "Medium", crust = "Thick")    // Overrides the crust
    orderPizza(size = "Small", toppings = "Veggie") // Overrides the toppings
}
Enter fullscreen mode Exit fullscreen mode

When Expressions

fun main() {
    val number = 3
    val result = when (number) {
        1 -> "One"
        2 -> "Two"
        3 -> "Three"
        else -> "Unknown"
    }
    println(result) // Output: Three
}
Enter fullscreen mode Exit fullscreen mode

Control flows

fun main() {
    val number = 4
    if (number % 2 == 0) {
        println("$number is even")
    } else {
        println("$number is odd")
    }
}
Enter fullscreen mode Exit fullscreen mode

Class

class Person(val name: String, val age: Int) {
    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

fun main() {
    val person = Person("Alice", 30)
    person.greet() // Output: Hello, my name is Alice and I am 30 years old.
}
Enter fullscreen mode Exit fullscreen mode

Private modifier

class Person(val name: String, private val age: Int) { // 'age' is private
    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

fun main() {
    val person = Person("Alice", 30)
    person.greet() // Output: Hello, my name is Alice and I am 30 years old.
    // println(person.age) // Error: Cannot access 'age': it is private
}
Enter fullscreen mode Exit fullscreen mode

Init block

The init block runs immediately after the primary constructor. It is useful for initializing additional properties or performing validations.

class Person(val name: String, val age: Int) {
    init {
        println("A new person named $name has been created!")
    }
}

fun main() {
    val person = Person("Alice", 30)
    // Output: A new person named Alice has been created!
}
Enter fullscreen mode Exit fullscreen mode

Inheritance

Inheritance allows a class to acquire properties and methods from another class.

open class Animal(val name: String) { // 'open' allows the class to be inherited
    fun eat() {
        println("$name is eating.")
    }
}

class Dog(name: String) : Animal(name) { // Dog inherits from Animal
    fun bark() {
        println("$name is barking.")
    }
}

fun main() {
    val dog = Dog("Buddy")
    dog.eat()  // Output: Buddy is eating.
    dog.bark() // Output: Buddy is barking.
}
Enter fullscreen mode Exit fullscreen mode

Abstract class

An abstract class cannot be instantiated and is meant to be subclassed. It can have both abstract (no body) and concrete methods.

abstract class Shape {
    abstract fun area(): Double
}

class Circle(val radius: Double) : Shape() {
    override fun area(): Double {
        return 3.14 * radius * radius
    }
}

fun main() {
    val circle = Circle(5.0)
    println("Area of the circle: ${circle.area()}") // Output: Area of the circle: 78.5
}
Enter fullscreen mode Exit fullscreen mode

Interface

An interface is like a contract. A class that implements an interface must provide implementations for its abstract methods.

interface Vehicle {
    fun drive()
}

class Car : Vehicle {
    override fun drive() {
        println("The car is driving.")
    }
}

fun main() {
    val car = Car()
    car.drive() // Output: The car is driving.
}
Enter fullscreen mode Exit fullscreen mode

Encapsulation

Encapsulation hides the internal state of an object and provides methods to access and modify it safely.

class Person(private var name: String, private var age: Int) {
    fun getName(): String {
        return name
    }

    fun getAge(): Int {
        return age
    }

    fun setName(newName: String) {
        name = newName
    }

    fun setAge(newAge: Int) {
        if (newAge > 0) {
            age = newAge
        } else {
            println("Invalid age.")
        }
    }
}

fun main() {
    val person = Person("John", 25)
    println("Name: ${person.getName()}")
}
Enter fullscreen mode Exit fullscreen mode

Lambda Expressions

Lambda expressions are a concise way to define small, anonymous functions.

val sum = { a: Int, b: Int -> a + b }
println(sum(2, 3)) // Output: 5
Enter fullscreen mode Exit fullscreen mode

Ex2:

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
   val count = numbers.count {x -> x % 2 == 0 }
    println("Count of even numbers: $count") // Output: Count of even numbers: 2
}
Enter fullscreen mode Exit fullscreen mode

Generics

Generics allow you to create classes, interfaces, and functions that can work with any data type. Here’s an example using a generic function:

fun <T> printElement(element: T) {
    println("Element: $element")
}

fun main() {
    printElement(10)        // Output: Element: 10
    printElement("Hello")   // Output: Element: Hello
    printElement(3.14)      // Output: Element: 3.14
}
Enter fullscreen mode Exit fullscreen mode

Anonymous Functions

Anonymous functions are functions that don't have a name. You can create them using the fun keyword. Here's a simple example:

fun main() {
    // Anonymous function assigned to a variable
    val multiply = fun(a: Int, b: Int): Int {
        return a * b
    }

    // Calling the anonymous function
    println(multiply(4, 5)) // Output: 20
}
Enter fullscreen mode Exit fullscreen mode

Data Class

A data class in Kotlin is used to hold data. It automatically provides toString(), equals(), hashCode(), and copy() methods for the class. Here's a simple example:

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

fun main() {
    val person1 = Person("Alice", 30)
    val person2 = Person("Bob", 25)

    println(person1) // Output: Person(name=Alice, age=30)
    println(person1 == person2) // Output: false

    // Copying person1 with a new name
    val person3 = person1.copy(name = "Charlie")
    println(person3) // Output: Person(name=Charlie, age=30)
}
Enter fullscreen mode Exit fullscreen mode

Sealed Class

A sealed class restricts class hierarchies to a limited set of subclasses. It is often used in situations like handling different types of results or states.

sealed class Result
data class Success(val message: String) : Result()
data class Error(val errorMessage: String) : Result()

fun main() {
    val result: Result = Success("Operation successful")

    when (result) {
        is Success -> println(result.message) // Output: Operation successful
        is Error -> println(result.errorMessage)
    }
}
Enter fullscreen mode Exit fullscreen mode

Companion Object

A companion object is a special object that is associated with a class and can hold functions and properties that are related to the class.

class MyClass {
    companion object {
        val constantValue = "I am constant"
    }
}

fun main() {
    // Accessing the companion object like a static member
    println(MyClass.constantValue) // Output: I am constant
}

Enter fullscreen mode Exit fullscreen mode

Extension Functions

Extension functions allow you to add new functions to existing classes without modifying their source code.

fun String.reverse(): String {
    return this.reversed()
}

fun main() {
    val original = "Kotlin"
    val reversed = original.reverse()
    println(reversed) // Output: niltoK
}
Enter fullscreen mode Exit fullscreen mode

Destructuring Declarations

Destructuring allows you to unpack a data class into its properties.

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

fun main() {
    val person = Person("Alice", 30)

    // Destructuring the object into variables
    val (name, age) = person
    println("Name: $name, Age: $age") // Output: Name: Alice, Age: 30
}
Enter fullscreen mode Exit fullscreen mode

Type Aliases

A type alias provides an alternative name for an existing type, making code easier to read and work with.

typealias StringList = List<String>

fun main() {
    val list: StringList = listOf("Kotlin", "Java", "Scala")
    println(list) // Output: [Kotlin, Java, Scala]
}

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
altamirobruno profile image
Altamiro Bruno

Very helpfull post! Thanks for sharing.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

Glad it was useful