Kotlin is a modern programming language that runs on the Java Virtual Machine (JVM). Google made it the official language for Android development in 2019. But Kotlin is not just for Android. You can use it for backend servers, desktop apps, and even web development.
In this tutorial, you will learn what Kotlin is, where it is used, and why so many developers prefer it over Java. You will also write your first Kotlin code.
What is Kotlin?
Kotlin is a programming language created by JetBrains. JetBrains is the company behind IntelliJ IDEA, one of the most popular code editors in the world.
Kotlin was first released in 2011 and reached version 1.0 in 2016. Here are the key facts:
- Runs on the JVM — Kotlin compiles to Java bytecode. This means it works anywhere Java works.
- 100% interoperable with Java — You can call Java code from Kotlin and Kotlin code from Java. You can mix both languages in one project.
- Concise — Kotlin needs much less code than Java for the same task.
- Safe — Kotlin prevents common bugs like NullPointerException at compile time.
- Open source — Kotlin is free and open source under the Apache 2.0 license.
Where is Kotlin Used?
Kotlin is a general-purpose language. Here are the main areas where developers use it:
Android Development — Google recommends Kotlin as the first choice for Android apps. Most new Android apps are written in Kotlin. Apps like Google Maps, Netflix, and Twitter use Kotlin.
Backend Development — Kotlin works with Spring Boot, Ktor, and other server frameworks. Many companies use Kotlin for their APIs and web services.
Kotlin Multiplatform (KMP) — Share code between Android, iOS, desktop, and web from a single Kotlin codebase. KMP is growing fast and is now stable.
Desktop Applications — With Compose for Desktop, you can build native desktop apps using Kotlin.
Scripting — You can use Kotlin as a scripting language, similar to Python or Bash.
Why Developers Love Kotlin
Let me show you real code examples. You will see why Kotlin is easier and safer than Java.
1. Less Code, Same Result
In Java, a simple class with some data looks like this:
// Java — 30+ lines for a simple data holder
public class User {
private String name;
private int age;
private String email;
public User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() { return name; }
public int getAge() { return age; }
public String getEmail() { return email; }
@Override
public boolean equals(Object o) { /* ... */ }
@Override
public int hashCode() { /* ... */ }
@Override
public String toString() { /* ... */ }
}
In Kotlin, the same thing is one line:
// Kotlin — 1 line does the same thing
data class User(val name: String, val age: Int, val email: String)
Kotlin generates equals(), hashCode(), toString(), and copy() for you. One line replaces 30+ lines of Java code.
2. Hello World
In Java:
// Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In Kotlin:
// Kotlin
fun main() {
println("Hello, World!")
}
No class needed. No public static void. No System.out. Just a function and println.
3. Type Inference
Kotlin is smart enough to figure out the type of your variable:
// You don't need to write the type — Kotlin knows it
val name = "Alex" // String
val age = 25 // Int
val height = 1.75 // Double
val isStudent = true // Boolean
println("Name: $name, Age: $age, Height: $height, Student: $isStudent")
You can still write the type if you want to be explicit. But most of the time, you don’t need to.
4. Null Safety
This is Kotlin’s best feature. In Java, any variable can be null, and this causes crashes:
// Java — this compiles fine but crashes at runtime
String name = null;
int length = name.length(); // NullPointerException!
In Kotlin, variables cannot be null by default:
// Kotlin — this does NOT compile
val name: String = null // Error: Null cannot be a value of a non-null type String
If you want a variable to be nullable, you must add ?:
// This compiles — ? means "this can be null"
val name: String? = null
// Safe call — returns null instead of crashing
println(name?.length) // prints: null
Kotlin catches null problems at compile time. Not at runtime when your app crashes.
5. String Templates
In Java, you concatenate strings with +:
// Java
String firstName = "Jordan";
String lastName = "Smith";
int age = 30;
System.out.println(firstName + " " + lastName + " is " + (age * 365) + " days old");
In Kotlin, you put variables directly inside strings:
// Kotlin
val firstName = "Jordan"
val lastName = "Smith"
val age = 30
// $ for variables, ${} for expressions
println("$firstName $lastName is ${age * 365} days old")
This is much easier to read and write.
6. When Expression
Java has switch, but Kotlin has when. It is much more powerful:
fun describeNumber(number: Int): String {
return when {
number < 0 -> "Negative"
number == 0 -> "Zero"
number in 1..10 -> "Small"
number in 11..100 -> "Medium"
else -> "Large"
}
}
println(describeNumber(5)) // Small
println(describeNumber(50)) // Medium
println(describeNumber(-3)) // Negative
println(describeNumber(500)) // Large
when can match on ranges, types, conditions, and more. It can also return a value.
7. Extension Functions
In Kotlin, you can add new functions to existing classes without modifying them:
// Add a wordCount() function to all Strings
fun String.wordCount(): Int {
return this.trim().split("\\s+".toRegex()).size
}
val sentence = "Kotlin is a modern programming language"
println(sentence.wordCount()) // 6
Try doing this in Java. You would need a utility class with static methods. In Kotlin, the function feels like it belongs to the String class.
8. Default Parameters
In Java, if you want optional parameters, you need multiple methods (method overloading):
// Java — need 3 methods for different parameter combinations
public String greet(String name) { return "Hello, " + name + "!"; }
public String greet(String name, String greeting) { return greeting + ", " + name + "!"; }
public String greet(String name, String greeting, String punctuation) {
return greeting + ", " + name + punctuation;
}
In Kotlin, one function handles everything:
fun greet(name: String, greeting: String = "Hello", punctuation: String = "!"): String {
return "$greeting, $name$punctuation"
}
println(greet("Alex")) // Hello, Alex!
println(greet("Sam", "Hi")) // Hi, Sam!
println(greet("Jordan", "Hey", "!!!")) // Hey, Jordan!!!
println(greet("Alex", punctuation = "?")) // Hello, Alex?
Default parameters and named arguments eliminate the need for method overloading.
Kotlin vs Java — Quick Comparison
| Feature | Java | Kotlin |
|---|---|---|
| Null safety | No (runtime crashes) | Yes (compile-time checks) |
| Data classes | Manual (30+ lines) | One line |
| Type inference | Limited | Full |
| Extension functions | No | Yes |
| String templates | No | Yes |
| Default parameters | No (need overloading) | Yes |
| Coroutines | No (need threads) | Yes (built-in) |
| Smart casts | No | Yes |
Should You Learn Kotlin?
Yes. Here is why:
- Android developers — Kotlin is the official language. Google builds all new Android features with Kotlin first.
- Java developers — Kotlin is easy to learn if you know Java. You can use both in the same project.
- Backend developers — Kotlin works with Spring Boot and is growing in the backend world.
- New programmers — Kotlin is easier to learn than Java. Less boilerplate, better error messages.
Kotlin is not replacing Java. Java is still everywhere. But Kotlin gives you a better experience with less code and fewer bugs.
Run the Example Code
You can see all the examples from this tutorial in one file:
fun main() {
println("=== KT-1: What is Kotlin? ===")
// Hello World
println("Hello, World!")
// Type Inference
val name = "Alex"
val age = 25
println("Name: $name, Age: $age")
// Null Safety
val nickname: String? = null
println("Nickname length: ${nickname?.length}")
// Data Classes
data class User(val name: String, val age: Int, val email: String)
val user = User("Alex", 25, "alex@example.com")
println(user)
// When Expression
val number = 42
val description = when {
number < 0 -> "Negative"
number == 0 -> "Zero"
number in 1..10 -> "Small"
number in 11..100 -> "Medium"
else -> "Large"
}
println("$number is $description")
}
In the next tutorial, you will install Kotlin and run this code on your computer.
Source Code
You can find the complete source code for this tutorial on GitHub:
What’s Next?
In the next tutorial, Kotlin Tutorial #2: Installing Kotlin and Your First Program, you will:
- Install IntelliJ IDEA
- Create your first Kotlin project
- Run your first program
- Try the Kotlin REPL and Kotlin Playground
This is part 1 of the Kotlin Tutorial series. Follow along to learn Kotlin from scratch. Need a quick reference? See the Kotlin Cheat Sheet.