Let's break down the "Hello World" program to understand Kotlin's fundamental concepts and syntax.
The Complete Program
fun main() {
println("Hello, World!")
}
Breaking Down Each Part
1. The `fun` Keyword
fun main() {
The fun keyword declares a function in Kotlin. All executable code in Kotlin must be inside functions.
Syntax:
fun functionName(parameters) {
// function body
}
2. The `main` Function
The main function is the entry point of every Kotlin application. When you run a Kotlin program, execution starts from the main function.
Two Valid Signatures:
// Modern syntax (Kotlin 1.3+)
fun main() {
println("No parameters needed")
}
// Classic syntax (still valid)
fun main(args: Array<String>) {
println("Can accept command-line arguments")
}
3. Function Parameters
In the classic syntax:
fun main(args: Array<String>)
-
args: Parameter name (can be any valid identifier) -
Array<String>: Type annotation - array of strings - Used to receive command-line arguments passed when running the program
Example with arguments:
fun main(args: Array<String>) {
if (args.isNotEmpty()) {
println("First argument: ${args[0]}")
}
}
Run with: java -jar program.jar Hello
4. The Return Type
By default, main returns Unit (Kotlin's equivalent of void in Java).
// These are equivalent:
fun main() { }
fun main(): Unit { }
Unit indicates the function doesn't return a meaningful value. Declaring it explicitly is optional.
5. The `println` Function
println("Hello, World!")
println is a built-in function that:
- Prints output to the standard output (console)
- Automatically adds a newline at the end
- Wraps Java's
System.out.printlnfor convenience
Related functions:
println("With newline") // Adds \n automatically
print("Without newline") // No newline added
6. String Literals
"Hello, World!"
Strings in Kotlin are enclosed in double quotes. Kotlin supports:
Regular strings:
val message = "Hello, World!"
String templates (interpolation):
val name = "Kotlin"
println("Hello, $name!") // Output: Hello, Kotlin!
Multi-line strings:
val text = """
Line 1
Line 2
Line 3
""".trimIndent()
7. Semicolons are Optional
Kotlin doesn't require semicolons at the end of statements:
// Both are valid:
println("Hello")
println("World")
// But semicolons work too:
println("Hello"); println("World");
Enhanced Examples
Example 1: Multiple Print Statements
fun main() {
println("Welcome to Kotlin!")
println("This is your first program.")
print("Kotlin is ")
print("awesome!")
}
Output:
Welcome to Kotlin!
This is your first program.
Kotlin is awesome!
Example 2: Using Variables
fun main() {
val language = "Kotlin"
val version = 1.9
println("Language: $language")
println("Version: $version")
}
Output:
Language: Kotlin
Version: 1.9
Example 3: Simple Calculation
fun main() {
val a = 10
val b = 5
val sum = a + b
println("$a + $b = $sum")
}
Output:
10 + 5 = 15
Example 4: Command-Line Arguments
fun main(args: Array<String>) {
println("Number of arguments: ${args.size}")
for (arg in args) {
println("Argument: $arg")
}
}
Run: java -jar program.jar Hello World 2024
Output:
Number of arguments: 3
Argument: Hello
Argument: World
Argument: 2024
Key Concepts Summary
| Concept | Description | Example |
|---|---|---|
fun |
Declares a function | fun main() |
main() |
Entry point of program | First function executed |
Unit |
Return type (like void) | Optional to declare |
println() |
Print with newline | println("text") |
print() |
Print without newline | print("text") |
| String Template | Embed variables in strings | "Hello, $name" |
| No Semicolons | Optional statement terminator | Clean syntax |
Common Beginner Mistakes
1. Missing `fun` keyword
// Wrong:
main() {
println("Hello")
}
// Correct:
fun main() {
println("Hello")
}
2. Incorrect function name
// Wrong (won't execute):
fun start() {
println("Hello")
}
// Correct:
fun main() {
println("Hello")
}
3. Missing parentheses
// Wrong:
println "Hello"
// Correct:
println("Hello")
Practice Exercises
- Modify the program to print your name
- Add multiple println statements
- Use string templates to print variables
- Try the program with command-line arguments
Next Steps
- Learn about variables (
valandvar) - Explore data types
- Understand functions in detail
- Practice with IDE for better development experience