Introduction to Kotlin
What is Kotlin, why Java developers should adopt it, and your first Hello World comparison.
What is Kotlin?
Kotlin is a modern, statically typed programming language developed by JetBrains (the creators of IntelliJ IDEA) and first released in 2016. It runs on the JVM, compiles to the same bytecode as Java, and was officially designated as Google's preferred language for Android development in 2019.
Kotlin is not a replacement for Java — it's a companion. You can have Java and Kotlin files in the same Android project, and Kotlin can call any Java library directly. This means you can migrate incrementally, file by file.
Kotlin vs Java: Key Differences at a Glance
| Feature | Java | Kotlin |
|---|---|---|
| Null Safety | No built-in null safety → NullPointerException at runtime | Compile-time null safety with ? types |
| Boilerplate | Getters, setters, constructors, toString() — all manual | Auto-generated with data class |
| Semicolons | Required at end of every statement | Optional (usually omitted) |
| Static members | static keyword |
companion object |
| Immutability | final keyword |
val (immutable) vs var (mutable) |
| String formatting | String.format("%s %d", name, age) |
"Hello $name, age $age" |
| Async code | Threads, ExecutorService, RxJava | Coroutines (structured concurrency) |
| Default class | Open (can extend by default) | Final by default (must mark open) |
| Checked exceptions | Enforced at compile time | Not required (unchecked only) |
Hello World: Java vs Kotlin
Let's start with the most fundamental program and immediately spot the differences:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
fun main() {
println("Hello, World!")
}
- No class wrapping needed —
fun main()is a top-level function println()replacesSystem.out.println()- No
public static, noString[] args, no semicolons - Kotlin 2.x:
main()with no parameters is valid
Where Does Kotlin Run?
Setting Up
If you already develop Android apps in Java, you're ready to start writing Kotlin today — no extra setup needed.
// app/build.gradle (existing Java project)
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}
// app/build.gradle.kts
plugins {
id("org.jetbrains.kotlin.android") version "2.2.0"
}
kotlin {
jvmToolchain(17)
}
// ✅ Now .kt files are recognized alongside .java
In Android Studio, you can instantly convert any Java file to Kotlin: open it, then go to Code → Convert Java File to Kotlin File (or press Ctrl+Alt+Shift+K). It's not always perfect, but it's a great learning tool.