Skip to content

devzique/how-to-code-kt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Kotlin: A Beginner’s Complete Guide

What is Kotlin?

Kotlin is a modern, statically-typed programming language developed by JetBrains. It is fully interoperable with Java and runs on the JVM (Java Virtual Machine). It is widely used for Android development, backend development, and more.


1. Basic Syntax

Variables

// Immutable (val) and mutable (var)
val name: String = "John" // Read-only
var age: Int = 25         // Mutable

// Type inference
val city = "New York"
var score = 100

Functions

// Basic function
fun greet(): String {
    return "Hello, World!"
}

// Single-expression function
fun square(x: Int) = x * x

// Function with default arguments
fun printMessage(message: String = "Hi") {
    println(message)
}

Control Flow

// If-else as an expression
val max = if (a > b) a else b

// When (like switch in Java)
when (x) {
    1 -> println("One")
    2 -> println("Two")
    else -> println("Other")
}

// Loops
for (i in 1..5) { // Range
    println(i)
}

while (x > 0) {
    println(x)
    x--
}

2. Classes and Objects

Defining a Class

class Person(val name: String, var age: Int) {
    fun introduce() {
        println("Hi, I am $name and I am $age years old.")
    }
}

val john = Person("John", 25)
john.introduce()

Inheritance

open class Animal {
    open fun sound() {
        println("Some generic sound")
    }
}

class Dog : Animal() {
    override fun sound() {
        println("Bark")
    }
}

val dog = Dog()
dog.sound()

3. Null Safety

var nullableName: String? = null

// Safe call
println(nullableName?.length)

// Elvis operator
val length = nullableName?.length ?: 0

// Non-null assertion
println(nullableName!!.length) // Throws exception if null

4. Collections

Lists

val items = listOf("apple", "banana", "cherry") // Immutable
val mutableItems = mutableListOf("apple", "banana") // Mutable
mutableItems.add("cherry")

Maps

val map = mapOf(1 to "one", 2 to "two") // Immutable
val mutableMap = mutableMapOf(1 to "one") // Mutable
mutableMap[2] = "two"

Filtering and Mapping

val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }
val squaredNumbers = numbers.map { it * it }

5. Higher-Order Functions

fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
    return operation(x, y)
}

val sum = calculate(5, 3) { a, b -> a + b }
println(sum) // Output: 8

6. Coroutines (Asynchronous Programming)

import kotlinx.coroutines.*

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

7. Interoperability with Java

// Calling Java from Kotlin
val list = ArrayList<String>()
list.add("Kotlin")

// Calling Kotlin from Java
public class Main {
    public static void main(String[] args) {
        Person person = new Person("John", 25);
        person.introduce();
    }
}

8. Extensions

fun String.capitalizeFirst(): String {
    return this.replaceFirstChar { it.uppercase() }
}

println("kotlin".capitalizeFirst()) // Output: Kotlin

9. DSLs (Domain Specific Languages)

Kotlin’s concise syntax is ideal for building DSLs.

fun html(block: Tag.() -> Unit): Tag {
    val tag = Tag("html")
    tag.block()
    return tag
}

class Tag(val name: String) {
    private val children = mutableListOf<Tag>()
    fun tag(name: String, block: Tag.() -> Unit) {
        val child = Tag(name)
        child.block()
        children.add(child)
    }
}

html {
    tag("head") {}
    tag("body") {
        tag("h1") {}
    }
}

10. Resources to Learn More


Dive into coding and enjoy Kotlin!

About

lol i wrote some small ahh doc on hot to code kt, but I used chatgpt to make it sound better idk if it fucked shit up coz I cant be bothered to check lol

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors