RizTech Academy logo
RizTech Academy
Getting StartedLesson 3 of 415 min

Your first Kotlin program

Every language has its "hello, world", and Kotlin's is worth more than a ritual — the few lines contain three ideas you will use in every program you ever write. This lesson writes it, runs it both ways, and reads it properly.

The program

Create a file called Hello.kt:

fun main() {
    val name = "Pune"
    println("Hello, $name!")
}

Three lines of body, and every token earns its place:

  • fun main() — fun declares a function; main is the special one the program starts from. When you run a Kotlin program, the JVM looks for main and begins there. Every runnable program has exactly one entry point, and this is it.
  • val name = "Pune" — val declares a value that cannot be reassigned (immutable — the next module's first lesson). Notice there is no type written: Kotlin infers String from the text on the right. And no semicolon — Kotlin does not require them.
  • println("Hello, $name!") — println prints a line to the console. The $name inside the string is a string template: Kotlin substitutes the value of name, so this prints Hello, Pune!. This is far more readable than gluing strings together with +, and you will use it constantly.

That is a complete, real Kotlin program. No class wrapper, no boilerplate — compared to Java's public static void main(String[] args) inside a class, Kotlin's fun main() is the whole thing, and that reduction of ceremony is the language's personality in miniature.

Running it from the command line

To see the compiler and runtime you set up actually work:

kotlinc Hello.kt -include-runtime -d hello.jar
java -jar hello.jar

The first command compiles Hello.kt into a runnable hello.jar (the -include-runtime flag bundles the Kotlin standard library so the jar runs anywhere Java does). The second runs it on the JVM. You will see:

Hello, Pune!

The compile step is noticeably slow the first time — the compiler has to start the JVM itself. That is normal, and it is exactly why you will not compile from the command line during real work; the IDE keeps a compiler warm in the background so edits feel instant.

Running it in IntelliJ — how you will actually work

In practice you run code in the IDE:

  1. Create a new Kotlin project (New Project → Kotlin, JVM, accept the JDK).
  2. Add a Kotlin file under src and paste the program.
  3. A small green ▶ arrow appears in the gutter next to fun main(). Click it and choose Run.

The output appears in a panel at the bottom. IntelliJ compiled and ran it for you — the same two steps as the command line, hidden behind one button, and much faster because the compiler was already running. The green arrow next to main is the thing you will click hundreds of times in this course.

Taking input, so it feels alive

A program that only prints is dull. Read a line from the user:

fun main() {
    print("What is your name? ")
    val name = readLine()
    println("Namaste, $name!")
}
  • print (no ln) writes without a newline, so the cursor stays on the same line for the answer — right for a prompt.
  • readLine() reads one line the user types. Run it, type Kavita, and it prints Namaste, Kavita!.

There is a subtlety hiding here that the whole null-safety module is about: readLine() can return nothing (if there is no input at all), so its type is String? — a nullable String, marked by the ?. That is why $name might one day print null. Do not fix it yet; just notice that Kotlin is already tracking "this might be absent" in the type. It is the language's defining idea, showing up in your second program.

Expressions everywhere — a taste

One more idea that makes Kotlin pleasant: many things that are statements in other languages are expressions in Kotlin — they produce a value. Even if:

fun main() {
    val hour = 20
    val greeting = if (hour < 12) "Good morning" else "Good evening"
    println(greeting)     // Good evening
}

In Java you would declare greeting and assign it inside each branch. In Kotlin the if itself is the value. This runs and prints Good evening. You will meet this properly in the basics module; for now, register that "returns a value" is the Kotlin default.

Check your work

What fun does. Declares a function.

What main is. The entry point — where the program starts.

Why val name = "Pune" needs no type. Kotlin infers String from the value; and no semicolon is needed.

What $name in a string does. String template — substitutes the value of name.

The two command-line steps and the tools. kotlinc compiles to a jar; java runs it on the JVM.

Why command-line compiling feels slow. The compiler starts its own JVM each time; the IDE keeps one warm.

How you run code in IntelliJ. The green ▶ arrow in the gutter beside main.

The difference between print and println. print omits the trailing newline.

Why readLine() returns String?. It might read nothing, so the type says "possibly absent" — Kotlin's null safety, already at work.

What makes if special in Kotlin. It is an expression — it produces a value.

Practice

  1. Write and run the first Hello.kt from the command line. Time how long the compile takes.
  2. Run the same program in IntelliJ with the green arrow. Note how much faster it feels, and why.
  3. Change "Pune" to your own city and confirm the output changes.
  4. Write the readLine() version and greet yourself by typing your name.
  5. Just press Enter without typing a name, and see what prints. You have met null — do not fix it yet.
  6. Write an if-expression that sets greeting based on the hour, and run it with two different hours.
  7. Break it on purpose: remove the closing } and read the compiler error. Errors are information, and reading them is a skill.

Official documentation

Next: scratch files, for experimenting without the ceremony of a project.

Stuck on this lesson?

Being stuck is part of it — but being stuck alone for three days is not. Our internship programme pairs this curriculum with code review and one-to-one help from working developers, and it is free.

About the internship