Chapter 2: First Kotlin Program & Basic Syntax
Now that we’ve set up IntelliJ IDEA with Kotlin (or you’re using Android Studio / VS Code), it’s time to write our very first Kotlin program and understand the beautiful, clean syntax that makes Kotlin so loved (especially in 2026!).
I’m going to explain everything like we’re sitting together in a cozy Mumbai café — slowly, clearly, with lots of real-life analogies, copy-paste examples, step-by-step breakdowns, common mistakes, and fun facts so it sticks perfectly.
Let’s write your first Kotlin program right now!
1. Hello World – The Classic First Program
Create a new Kotlin file in IntelliJ:
- Right-click on src folder → New → Kotlin File/Class → name it HelloWorld
- Choose File (not Class)
The shortest possible Hello World in Kotlin:
|
0 1 2 3 4 5 6 7 8 |
fun main() { println("Hello, Webliance! Welcome to Kotlin in 2026! 🚀") } |
That’s it!
- No class declaration needed
- No semicolons
- No public static void main(String[] args) ceremony
Run it → Right-click file → Run ‘HelloWorldKt.main()’
Output:
|
0 1 2 3 4 5 6 |
Hello, Webliance! Welcome to Kotlin in 2026! 🚀 |
2. The main() Function – Different Styles
Kotlin is very flexible with how you write the entry point.
Style 1: Simplest (most common in modern Kotlin)
|
0 1 2 3 4 5 6 7 8 |
fun main() { println("Hi from the shortest main!") } |
Style 2: Traditional Java-like style (with arguments)
|
0 1 2 3 4 5 6 7 8 |
fun main(args: Array<String>) { println("Program arguments: ${args.joinToString()}") } |
Run with arguments (in IntelliJ → Run → Edit Configurations → Program arguments: hello world)
Output:
|
0 1 2 3 4 5 6 |
Program arguments: hello, world |
Style 3: With explicit return type (rarely needed)
|
0 1 2 3 4 5 6 7 8 |
fun main(): Unit { println("I explicitly return Unit") } |
Unit is Kotlin’s equivalent of void — but you almost never write it.
3. Packages & Imports
Packages in Kotlin work exactly like Java — folders + package keyword.
Example file structure:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
src/ └── main/ └── kotlin/ └── com/ └── webliance/ └── basics/ └── HelloWorld.kt |
Inside HelloWorld.kt:
|
0 1 2 3 4 5 6 7 8 9 10 |
package com.webliance.basics fun main() { println("Hello from package com.webliance.basics!") } |
Importing:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
import java.time.LocalDateTime import kotlin.math.sqrt fun main() { println("Current time: $LocalDateTime.now()") println("Square root of 25: ${sqrt(25.0)}") } |
Wildcard import (use sparingly):
|
0 1 2 3 4 5 6 |
import java.util.* |
Important: Kotlin auto-imports many things:
- kotlin.*
- kotlin.annotation.*
- kotlin.collections.*
- kotlin.io.*
- etc.
So you rarely need to write import kotlin.collections.List — it’s automatic!
4. Comments, Whitespace & Semicolons (Optional!)
Comments – same as Java, but Kotlin also has KDoc (like Javadoc)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Single-line comment /* Multi-line comment can span multiple lines */ /** * KDoc comment – appears in documentation * @param name The person's name * @return Greeting message */ fun greet(name: String): String { return "Hello, $name!" } |
Whitespace & Indentation Kotlin is very flexible — indentation is just for readability (no {} required for single-line blocks)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun main() { if (true) println("Single line – no braces needed!") if (true) { println("Multiple lines – use braces") println("Second line") } } |
Semicolons are OPTIONAL! You can write:
|
0 1 2 3 4 5 6 7 8 9 |
fun main() { println("No semicolon") println("Still no semicolon") } |
But most Kotlin developers don’t use semicolons unless needed (e.g., multiple statements on one line).
5. Basic Program Structure (What a Real Kotlin File Looks Like)
Minimal structure:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.webliance.basics // Imports (optional – many are auto-imported) import java.time.LocalDate /** * Entry point of the program */ fun main() { // Your code here println("Hello, Kotlin World!") val today = LocalDate.now() println("Today is: $today") } |
Real-world style (with top-level functions, constants, etc.):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.webliance.app const val APP_NAME = "My Awesome App" const val VERSION = "1.0.0" fun main() { println("$APP_NAME v$VERSION") greetUser("Webliance") } fun greetUser(name: String) { println("नमस्ते $name! स्वागत आहे Kotlin मध्ये!") } |
6. Quick Recap Table (Your Cheat Sheet)
| Topic | Kotlin Way (Best Practice) | Java Equivalent (for comparison) |
|---|---|---|
| Entry point | fun main() or fun main(args: Array<String>) | public static void main(String[] args) |
| Package declaration | package com.example.app | Same |
| Imports | import java.time.LocalDate (many auto-imported) | Same, but no auto-imports |
| Semicolons | Optional – almost never used | Mandatory |
| Comments | //, /* */, /** KDoc */ | Same |
| Code blocks | Braces optional for single-line | Always required |
7. Common Newbie Mistakes & Fixes
| Mistake | Problem | Fix |
|---|---|---|
| Writing public static void main() | Compile error – Kotlin doesn’t need it | Just write fun main() |
| Forgetting fun keyword | Syntax error | Always start function with fun |
| Putting semicolon everywhere | Looks like Java – not idiomatic | Remove most semicolons – Kotlin is cleaner |
| Using ClassName.main() to run | Confusing in IntelliJ | Just right-click file → Run |
| Not using string templates | Verbose code | Use $variable or ${expression} |
8. Homework for You (Let’s Make It Fun!)
- Basic Write a program with fun main() that prints your name, city, and current year using string templates.
- Medium Create two functions:
- fun greet(name: String) → prints “नमस्ते $name!”
- fun add(a: Int, b: Int) → returns sum Call both from main().
- Advanced Write a program that asks user for name and age (using readln()) → prints greeting + “You are ${age} years old!”
- Fun Make a program that prints today’s date in 4 different formats using LocalDate.now() and format().
- Challenge Create a top-level constant APP_VERSION = “1.0.0” and use it in the welcome message.
You’ve just written your very first Kotlin program — celebrate with some cutting chai! ☕
