iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

Kotlin is a modern JVM language designed for Android and beyond. Concise, null-safe, fully interop with Java, and increasingly used on the server.

Kotlin — what it is

EXAMPLE
// ===== The values =====
// - Concise syntax (val, data class, when, lambdas)
// - Null safety in the type system (String? vs String)
// - Coroutines for structured concurrency
// - 100% Java interop; runs on JVM, also Android, Kotlin/Native, Kotlin/JS

// ===== Hello, world =====
fun main() {
    println("hello, world")
}

// Run: kotlinc hello.kt -include-runtime -d hello.jar && java -jar hello.jar

// ===== Data class + when =====
data class User(val id: Int, val name: String, val email: String)

fun greet(u: User) = when {
    u.name.isEmpty() -> "Hi there"
    else -> "Hi ${u.name}"
}

// ===== Coroutines =====
import kotlinx.coroutines.*

suspend fun work(): String {
    delay(100)
    return "done"
}

fun main() = runBlocking {
    val results = listOf(
        async { work() },
        async { work() },
    ).awaitAll()
    println(results)
}

// ===== Android (a flavour, not the whole) =====
class MainActivity : AppCompatActivity() {
    override fun onCreate(b: Bundle?) {
        super.onCreate(b)
        setContent {
            MaterialTheme {
                Text("hello, Compose")
            }
        }
    }
}

// ===== When Kotlin wins =====
// - Android (Google's preferred language)
// - JVM backends (Ktor, Spring with Kotlin, http4k)
// - Replacing Java incrementally
// - Multiplatform (KMP) where shared code maps cleanly

// ===== When Kotlin hurts =====
// - Tiny CLIs (slow JVM cold start; reach for Native or another lang)
// - Mixed-language teams without Kotlin familiarity
// - Heavily reflection-based Java APIs (some friction)

// ===== Patterns to internalise =====
// - val by default; var only when needed
// - Nullable types in signatures; resolve at boundaries
// - data class for value types
// - Coroutines with structured scopes; never GlobalScope

// ===== Pitfalls =====
// - !! everywhere -> defeats the null-safety wins
// - Long extension function chains that hide intent
// - lateinit on a non-null var where lazy or constructor injection would fit
// - Mixing Java and Kotlin null conventions without @Nullable annotations

Why it matters

Kotlin is a Java that fits in your head. Null safety, data classes, when expressions, coroutines — modern features that compile to bytecode and interop perfectly with the JVM ecosystem. Android picked it; servers are following. A great default if you already live on the JVM.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// Kotlin: statically typed, runs on JVM (also Native + JS).
// Google's preferred language for Android. By JetBrains.
Try it Yourself »

Test yourself

Q1. Kotlin is developed by…
Q2. Kotlin compiles to…
Q3. Google declared Kotlin the preferred language for…

Discussion

Loading…