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()—fundeclares a function;mainis the special one the program starts from. When you run a Kotlin program, the JVM looks formainand begins there. Every runnable program has exactly one entry point, and this is it.val name = "Pune"—valdeclares a value that cannot be reassigned (immutable — the next module's first lesson). Notice there is no type written: Kotlin infersStringfrom the text on the right. And no semicolon — Kotlin does not require them.println("Hello, $name!")—printlnprints a line to the console. The$nameinside the string is a string template: Kotlin substitutes the value ofname, so this printsHello, 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:
- Create a new Kotlin project (New Project → Kotlin, JVM, accept the JDK).
- Add a Kotlin file under
srcand paste the program. - 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(noln) 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, typeKavita, and it printsNamaste, 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
- Write and run the first
Hello.ktfrom the command line. Time how long the compile takes. - Run the same program in IntelliJ with the green arrow. Note how much faster it feels, and why.
- Change
"Pune"to your own city and confirm the output changes. - Write the
readLine()version and greet yourself by typing your name. - Just press Enter without typing a name, and see what prints. You have met
null— do not fix it yet. - Write an
if-expression that setsgreetingbased on the hour, and run it with two different hours. - Break it on purpose: remove the closing
}and read the compiler error. Errors are information, and reading them is a skill.
Official documentation
- Kotlin — Basic syntax —
fun main,val,printlnand string templates, all in one place. - Kotlin — Hello world — The official first program, with an in-browser runner.
- Kotlin — Command-line compiler — The
kotlinc … -include-runtimecommand used here. - Kotlin Playground — Run Kotlin in the browser with nothing installed, useful for a quick check.
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