Featured image of post Kotlin in a Nutshell

Kotlin in a Nutshell

With Code Examples

Kotlin is a modern, programming language that runs on the Java Virtual Machine (JVM).

It’s designed to be fully interoperable with Java while improving upon its verbosity and complexity.

Think “Typescript-JavaScript” relationship… In a way

Biggest thing you will notice is a much leaner syntax..

1. Hello, World!

1
2
3
fun main() {
    println("Hello, World!")
}

This is the most basic Kotlin program. The main function is the entry point, and println outputs text to the console.

2. Variables and Constants

1
2
3
val name = "Kotlin"
var age = 10
age += 1

val declares a read-only variable (constant), while var allows modification.

3. Functions

1
2
3
fun greet(name: String): String {
    return "Hello, $name!"
}

Kotlin functions are concise and support string interpolation with $name.

4. Null Safety

1
2
var nullableString: String? = null
nullableString?.let { println(it.length) }

Using ? prevents NullPointerException by enabling safe access.

5. Conditional Expressions

1
2
val number = 10
val result = if (number > 0) "Positive" else "Negative"

if in Kotlin is an expression that returns a value.

6. Loops

1
2
3
for (i in 1..5) {
    println(i)
}

Kotlin supports range-based loops with ...

7. When Expression

1
2
3
4
5
6
val grade = "A"
val message = when (grade) {
    "A" -> "Excellent!"
    "B" -> "Good job!"
    else -> "Keep trying!"
}

when replaces switch statements with a more expressive syntax.

8. Classes and Objects

1
2
3
4
class Person(val name: String, var age: Int)

val person = Person("Alice", 25)
println(person.name)

Kotlin simplifies class definitions with primary constructors.

9. Extension Functions

1
2
fun String.shout() = this.uppercase()
println("hello".shout())

You can add new functions to existing classes without modifying them.

10. Coroutines

1
2
3
4
5
6
7
8
9
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(1000L)
        println("Coroutine Example")
    }
    println("Start")
}

Coroutines provide a lightweight way to handle asynchronous operations.


Key Ideas Table

Key IdeaSummary
Kotlin OverviewKotlin is a modern and expressive language for JVM, Android, and more
Hello WorldA simple program to print text to the console
Variablesval for constants, var for mutable variables
FunctionsKotlin functions are concise and support string interpolation
Null SafetyPrevents NullPointerException with safe calls and the Elvis operator
Conditionalsif expressions return values, when replaces switch statements
LoopsKotlin supports range-based loops
ClassesKotlin simplifies class definitions with primary constructors
Extension FunctionsAdd functions to existing classes without modifying them
CoroutinesLightweight threading with structured concurrency

References

  1. Official Kotlin Website
  2. Kotlin Documentation
  3. JetBrains Kotlin Blog
  4. Google Developers: Kotlin for Android
  5. Coroutines Guide