Your First Kotlin Program: main, println & Input
Write a complete Hello World, read console input, and print formatted output.
Your First Kotlin Program: main, println & Input is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The main Function
Every standalone Kotlin program starts at fun main(). The JVM calls this function when the program launches.
fun main() {
// your code here
}Hello, World
The classic first program. println writes a line of text to the console followed by a newline.
fun main() {
println("Hello, World!")
}println vs print
println adds a newline at the end. print does not. Use print when you want consecutive output on one line.
fun main() {
print("Hello, ")
print("Kotlin")
println("!") // newline after
println("Next line")
}Variables and Printing
Declare a variable with val (read-only) or var (mutable), then embed it in a string using $.
fun main() {
val name = "Alice"
val age = 30
println("Name: $name, Age: $age")
}Multi-Argument println
You can also concatenate values with the + operator, though string templates are usually cleaner.
fun main() {
val city = "Istanbul"
val temp = 22
println("Weather in " + city + ": " + temp + "C")
}Reading Console Input
readLine() reads a line of text from standard input. It returns String? (nullable) — use !! or a fallback to get a non-null value.
fun main() {
println("Enter your name:")
val name = readLine() ?: "Stranger"
println("Hello, $name!")
}readln() (Kotlin 1.6+)
Modern Kotlin offers readln() which returns a non-null String and throws if input is closed — cleaner for simple programs.
fun main() {
println("Type something:")
val line = readln()
println("You typed: $line")
}Parsing Numbers from Input
Console input arrives as String. Convert with toInt() or toIntOrNull() for safe parsing.
fun main() {
println("Enter your age:")
val ageInput = readln()
val age = ageInput.toIntOrNull() ?: 0
println("Next year you will be ${age + 1}")
}Multiple Inputs
Call readln() multiple times to gather more than one value.
fun main() {
println("First name:")
val first = readln()
println("Last name:")
val last = readln()
println("Full name: $first $last")
}Formatting Output
Use string templates with ${...} to embed any expression — arithmetic, function calls, member access.
fun main() {
val price = 19.99
val quantity = 3
println("Total: ${price * quantity}")
println("Items: ${quantity}x")
}main with Arguments
fun main(args: Array<String>) receives command-line arguments. Useful for CLI tools.
fun main(args: Array<String>) {
if (args.isEmpty()) {
println("No args passed")
} else {
println("First arg: ${args[0]}")
}
}Quick Check
Which function reads a non-null line from standard input in modern Kotlin?
Recap
fun main() is the entry point. println prints with a newline, print without. Read input with readln() (non-null) or readLine() (nullable). String templates with $ and ${...} make output concise.
Frequently asked questions
Is the “Your First Kotlin Program: main, println & Input” lesson free?
Yes — the full text of “Your First Kotlin Program: main, println & Input” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Your First Kotlin Program: main, println & Input”?
Write a complete Hello World, read console input, and print formatted output. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Your First Kotlin Program: main, println & Input” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up Kotlin: REPL, IntelliJ & Online Playground
- val vs var: Immutability From Day One
- Kotlin Basic Types: Int, Double, Boolean, Char
- Your First Kotlin Program: main, println & Input