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:

Kotlin

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:

text

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)

Kotlin

Style 2: Traditional Java-like style (with arguments)

Kotlin

Run with arguments (in IntelliJ → Run → Edit Configurations → Program arguments: hello world)

Output:

text

Style 3: With explicit return type (rarely needed)

Kotlin

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:

text

Inside HelloWorld.kt:

Kotlin

Importing:

Kotlin

Wildcard import (use sparingly):

Kotlin

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)

Kotlin

Whitespace & Indentation Kotlin is very flexible — indentation is just for readability (no {} required for single-line blocks)

Kotlin

Semicolons are OPTIONAL! You can write:

Kotlin

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:

Kotlin

Real-world style (with top-level functions, constants, etc.):

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!)

  1. Basic Write a program with fun main() that prints your name, city, and current year using string templates.
  2. Medium Create two functions:
    • fun greet(name: String) → prints “नमस्ते $name!”
    • fun add(a: Int, b: Int) → returns sum Call both from main().
  3. Advanced Write a program that asks user for name and age (using readln()) → prints greeting + “You are ${age} years old!”
  4. Fun Make a program that prints today’s date in 4 different formats using LocalDate.now() and format().
  5. 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! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *