First Program Concept

Let's break down the "Hello World" program to understand Kotlin's fundamental concepts and syntax.

The Complete Program

Example

fun main() {
    println("Hello, World!")
}

Breaking Down Each Part

1. The `fun` Keyword

Example

fun main() {

The fun keyword declares a function in Kotlin. All executable code in Kotlin must be inside functions.

Syntax:

Example

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:

Example

// 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:

Example

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:

Example

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

Example

// 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

Example

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.println for convenience

Related functions:

Example

println("With newline")  // Adds \n automatically
print("Without newline")  // No newline added

6. String Literals

Example

"Hello, World!"

Strings in Kotlin are enclosed in double quotes. Kotlin supports:

Regular strings:

Example

val message = "Hello, World!"

String templates (interpolation):

Example

val name = "Kotlin"
println("Hello, $name!")  // Output: Hello, Kotlin!

Multi-line strings:

Example

val text = """
    Line 1
    Line 2
    Line 3
""".trimIndent()

7. Semicolons are Optional

Kotlin doesn't require semicolons at the end of statements:

Example

// Both are valid:
println("Hello")
println("World")

// But semicolons work too:
println("Hello"); println("World");

Enhanced Examples

Example 1: Multiple Print Statements

Example

fun main() {
    println("Welcome to Kotlin!")
    println("This is your first program.")
    print("Kotlin is ")
    print("awesome!")
}

Output:

Output

Welcome to Kotlin!
This is your first program.
Kotlin is awesome!

Example 2: Using Variables

Example

fun main() {
    val language = "Kotlin"
    val version = 1.9
    println("Language: $language")
    println("Version: $version")
}

Output:

Output

Language: Kotlin
Version: 1.9

Example 3: Simple Calculation

Example

fun main() {
    val a = 10
    val b = 5
    val sum = a + b
    println("$a + $b = $sum")
}

Output:

Output

10 + 5 = 15

Example 4: Command-Line Arguments

Example

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:

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

Example

// Wrong:
main() {
    println("Hello")
}

// Correct:
fun main() {
    println("Hello")
}

2. Incorrect function name

Example

// Wrong (won't execute):
fun start() {
    println("Hello")
}

// Correct:
fun main() {
    println("Hello")
}

3. Missing parentheses

Example

// Wrong:
println "Hello"

// Correct:
println("Hello")

Practice Exercises

  1. Modify the program to print your name
  2. Add multiple println statements
  3. Use string templates to print variables
  4. Try the program with command-line arguments
  5. Next Steps

  • Learn about variables (val and var)
  • Explore data types
  • Understand functions in detail
  • Practice with IDE for better development experience

Input Required

This code uses input(). Please provide values below:

🤖 Coding Mentor
🤖

Hi! I'm your coding mentor

Ask me anything about programming:

• Python, Java, C++, JavaScript

• Algorithms & Data Structures

• Debugging & Code Help