Introduction to Kotlin
Reviewed & published by Brayan K
Kotlin is a modern, concise programming language that runs on the JVM and is Google's preferred language for Android development.
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
By the end of this lesson you'll understand what Kotlin is and why it exists, how it improves on Java with conciseness and null safety, and you'll have written and run your own first Kotlin program that prints to the console.
What You'll Learn in This Lesson
- What Kotlin is and why JetBrains created it
- How Kotlin runs on the JVM and interoperates with Java
- Why Google made Kotlin its preferred Android language
- How to print output to the console with println
- How to use string templates with $ and ${...}
- How to write and run your first main() function
1️⃣ What is Kotlin?
Kotlin was created by JetBrains (the company behind IntelliJ IDEA) and first appeared in 2011. It was designed to fix the everyday frustrations of writing Java while staying 100% compatible with it. It rests on a few key ideas:
- • Concise — common tasks take far less code than Java, so there is less to read, write, and get wrong.
- • Null-safe — the type system separates values that can be null from those that cannot, eliminating NullPointerExceptions at compile time.
- • Interoperable — Kotlin and Java code can live side by side in the same project and call each other freely.
- • Multiplatform — the same language compiles to the JVM, JavaScript, and native code for iOS, Android, and desktop.
Because of this, Kotlin powers a huge amount of modern software — especially Android apps and back-end servers. You'll start where every Kotlin developer starts: a tiny program that prints text. Read the worked example below first — every line is explained — then run it yourself.
// This is a Kotlin program. Save it in a file called Main.kt
// Every runnable Kotlin program starts inside the main() function.
fun main() {
// println writes a line of text to the console (standard output).
println("Hello, Kotlin!") // -> Hello, Kotlin!
println("Welcome to the JVM.") // -> Welcome to the JVM.
// Kotlin does real work too — arithmetic runs as you'd expect.
val total = 2 + 2
println("2 + 2 = $total") // -> 2 + 2 = 4
}2️⃣ Why Kotlin? Conciseness, Templates, and Null Safety
Two things make Kotlin feel different from Java straight away. First, type inference means you rarely write types — the compiler figures them out. Second, string templates let you embed values directly in text with $, and a full expression with ${...}. Read the comments, predict the output, then check.
fun main() {
// Kotlin is CONCISE — type inference means you rarely write types.
val language = "Kotlin" // the compiler knows this is a String
val year = 2011 // and this is an Int
// String templates let you drop a value straight into text with $.
println("$language first appeared in $year.")
// For a full expression inside a template, wrap it in ${...}
println("That was ${2026 - year} years ago.")
// Kotlin is null-safe: a normal String can NEVER hold null,
// so a whole class of crashes is impossible by default.
val name: String = "Ada"
println("Hello, $name! Length = ${name.length}")
}Notice how short that was: no type declarations, no + string concatenation, no semicolons. A val is a read-only variable (like final in Java), and the template syntax keeps the text readable. That conciseness is a big part of why developers love Kotlin.
Your turn. The program below works once you fill in the parts marked TODO. Follow the 👉 hints, then run it and compare with the expected output.
// 🎯 YOUR TURN — replace each TODO then run main()
fun main() {
// 1) Store your name in a read-only variable (use val)
val name = "TODO" // 👉 put your name in "double quotes"
// 2) Store your favourite number
val number = 0 // 👉 change 0 to any whole number
// 3) Print a greeting that includes both, using string templates.
// Remember: $name drops a value in, ${...} drops in an expression.
println("Hi, $name! Your number doubled is ${number * 2}.")
// ✅ Expected (example): Hi, Sam! Your number doubled is 14.
}3️⃣ Running a Kotlin Program
Every standalone Kotlin program needs a main() function — that's where execution begins. To run a program on your own machine, save it in a file ending in .kt (for example Main.kt), then compile and run it. The Kotlin Playground does this for you with one click.
# Check the Kotlin compiler is installed (prints the version):
kotlinc -version
# Compile Main.kt into a runnable jar:
kotlinc Main.kt -include-runtime -d main.jar
# Run it with the JVM:
java -jar main.jar
# (the line below is the program's output)Mini-Challenge: An "About Me" Program
No blanks this time — just a brief and an outline. Write it yourself, run it, and check your output against the example in the comments. This is exactly the kind of tiny program that builds real fluency.
Common Errors (and the fix)
- "No main function found" — Every runnable program needs a fun main(). Make sure your code is inside it, not at the top level on its own.
- Forgetting the $ in a template — Writing "Hello name" prints the literal word "name". Use "Hello $name" to insert the variable's value.
- Using ${ } when $ would do — For a plain variable just use $name. Reserve the braces ${...} for a full expression like ${a + b}.
- Adding semicolons everywhere — Kotlin does not need semicolons at the end of lines. They are allowed but unnecessary; leave them out.
Pro Tips
- 💡 Use the Playground first: play.kotlinlang.org runs code instantly so you can experiment with zero setup.
- 💡 Prefer val: reach for val (read-only) by default and only use var when you truly need to reassign. You'll learn why in the next lesson.
- 💡 Let the compiler infer types: you usually don't need to write : String or : Int — Kotlin figures it out.
📋 Quick Reference — Kotlin Basics
| Concept | Kotlin Syntax |
|---|---|
| Program entry point | fun main() { } |
| Print a line | println("hi") |
| Read-only variable | val x = 5 |
| Insert a variable | "Hi $name" |
| Insert an expression | "${a + b}" |
| File extension | .kt |
🎉 Lesson Complete!
- ✅ Kotlin is a modern, concise language created by JetBrains
- ✅ It runs on the JVM and is fully interoperable with Java
- ✅ Google made it the preferred language for Android
- ✅ println prints to the console; main() is the entry point
- ✅ String templates use $name and ${...}
Practice quiz
What company created the Kotlin language?
- JetBrains
- Oracle
- Microsoft
Answer: JetBrains. Kotlin was created by JetBrains, the company behind IntelliJ IDEA.
Where does the execution of a standalone Kotlin program begin?
- The first println
- The start() method
- The main() function
- The top of the file
Answer: The main() function. Every runnable Kotlin program starts inside the main() function.
Which function writes a line of text to the console?
- println
- echo
- console.log
- print.line
Answer: println. println writes a line of text to standard output.
What file extension does a Kotlin source file use?
- .kotlin
- .kts only
- .kt
- .java
Answer: .kt. Kotlin source files end in .kt (scripts use .kts).
In a string template, how do you insert a plain variable named name?
- {name}
- %name%
- $name
- #name
Answer: $name. A leading $ inserts a variable's value: "Hello $name".
How do you embed a full expression inside a string template?
- ${...}
- $(...)
- @{...}
- <%...%>
Answer: ${...}. Wrap a full expression in ${...}, for example ${2026 - year}.
What keyword declares a read-only variable in Kotlin?
- let
- const
- var
- val
Answer: val. val declares a read-only variable, similar to final in Java.
On which platform does Kotlin most commonly run?
- The JVM
- Only Android hardware
- The .NET CLR
- A custom Kotlin VM
Answer: The JVM. Kotlin most commonly compiles to JVM bytecode and runs on the JVM.
How does Kotlin relate to Java?
- It replaces the JVM entirely
- It is fully interoperable with Java
- It cannot call Java code
- It only runs in browsers
Answer: It is fully interoperable with Java. Kotlin is fully interoperable with Java and shares the JVM.
Does Kotlin require a semicolon at the end of each line?
- Yes, always
- No, semicolons are optional
- Only inside main()
- Only for println calls
Answer: No, semicolons are optional. Kotlin does not require line-ending semicolons; they are optional.
Continue this course
- Next: Variables (val & var) — Declare read-only and mutable variables with type inference
- Quick reference: Kotlin cheat sheet › Basics & Variables
Frequently asked questions
What exactly is Kotlin — and how is it different from Java?
Kotlin is a modern, statically typed programming language created by JetBrains that runs on the Java Virtual Machine (JVM). It is fully interoperable with Java — you can call Java code from Kotlin and vice versa — so it slots into existing projects. The big differences are that Kotlin is far more concise (less boilerplate), and it has built-in null safety that prevents the NullPointerExceptions that plague Java. In 2019 Google made Kotlin its preferred language for Android development, which is why it has become so popular.
Do I need to know Java before learning Kotlin?
No. Kotlin is often easier to learn as a first language than Java because it removes a lot of ceremony — there is no need for semicolons, you rarely write explicit types thanks to type inference, and many common tasks take a single line. Knowing Java helps you appreciate what Kotlin improves, but it is not required. This course assumes no prior Kotlin or Java experience.
Where does Kotlin code actually run?
Most commonly Kotlin compiles to JVM bytecode, the same format Java produces, so it runs anywhere a JVM runs — servers, desktops, and Android phones. Kotlin can also compile to JavaScript (Kotlin/JS) for the web and to native machine code (Kotlin/Native) for iOS and other platforms. In this course we focus on Kotlin on the JVM, which is the most common starting point.
How do I run a Kotlin program without installing anything?
The fastest way to experiment is the official online editor at play.kotlinlang.org (the Kotlin Playground), which runs your code in the browser. To run Kotlin locally, install IntelliJ IDEA (free Community edition) or the standalone Kotlin compiler, save your code in a .kt file, and run the main() function. Every example in this lesson shows the exact output so you can always check your work.