Hello World Program - Kotlin Tutorial
Kotlin Course / Introduction / Hello World Program

Hello World Program

BLUF: Mastering Hello World Program is a fundamental step in modern Android and server-side development. This lesson covers the concise syntax and powerful features of Kotlin that make this concept easy to implement.
Modern Expressive Code: Hello World Program

Kotlin's safety features and expressive syntax reduce boilerplate. See how Hello World Program makes your code cleaner and more reliable in the tutorial below.

The "Hello World" program is the traditional first program when learning a new language. This guide shows you how to write, compile, and run a Kotlin program from the command line.

Writing Your First Kotlin Program

Step 1: Create a new file named hello.kt using any text editor (Notepad++, VS Code, Sublime Text, etc.)

Step 2: Add the following code:

Example

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

Understanding the Code

  • fun main: Entry point of the Kotlin application
  • println: Built-in function to print output to console
  • No semicolons required (optional in Kotlin)
  • .kt extension is used for all Kotlin source files
  • Compiling the Program

Step 1: Open terminal/command prompt and navigate to the directory containing hello.kt

Example

cd path/to/your/file

Step 2: Compile the Kotlin file to a JAR:

Example

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

Explanation of flags:

  • kotlinc: Kotlin compiler command
  • -include-runtime: Packages Kotlin runtime library in the JAR
  • -d hello.jar: Output file name (destination)
  • Compilation Output

If successful, you'll see a hello.jar file created in the same directory. No output means compilation succeeded.

Common Compilation Errors

Error: "kotlinc is not recognized"

  • Solution: Kotlin compiler is not in PATH. Verify installation.

Error: Syntax errors

  • Check for typos in function name or missing braces
  • Ensure proper code structure
  • Running the Program

Execute the compiled JAR file:

Example

java -jar hello.jar

Expected Output:

Output

Hello, World!

Alternative: Direct Execution (Kotlin Script)

You can also run Kotlin code directly without creating a JAR:

Create hello.kts (Kotlin Script):

Example

println("Hello, World!")

Run directly:

Example

kotlinc -script hello.kts

This is useful for quick testing and scripting.

Complete Workflow Summary

Step Command Description
1. Write Create hello.kt Write Kotlin source code
2. Compile kotlinc hello.kt -include-runtime -d hello.jar Compile to executable JAR
3. Run java -jar hello.jar Execute the program

Enhanced Example: Custom Message

Let's create a more interactive program:

Example

fun main() {
    val name = "Kotlin"
    println("Hello, $name!")
    println("Welcome to Kotlin programming.")
    println("Current year: ${2024}")
}

Output:

Output

Hello, Kotlin!
Welcome to Kotlin programming.
Current year: 2024

Tips for Command Line Development

  1. First compilation is slow: JVM warmup takes time
  2. Use Kotlin daemon: Faster subsequent compilations
Example

   kotlinc -daemon hello.kt
  1. Clean build: Delete old JAR files before recompiling
  2. Check version: Verify Kotlin version with kotlinc -version
  3. Next Steps

  • Learn about Kotlin program structure and concepts
  • Set up an IDE for better development experience
  • Explore variables, data types, and functions

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience