Kotlin Hello World Program


Writing Your First Kotlin Program

Create a file named Hello.kt. In Kotlin, the filename is flexible, but it’s a good practice to use a name that matches the class or function inside.

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

Compiling the Program

If you are using the command line, save the file as Hello.kt and run:

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

What happens during compilation?

The kotlinc command invokes the Kotlin compiler:

  • Reads your .kt source file — written in Kotlin code.
  • Compiles it to Java bytecode, and optionally includes the Kotlin runtime.
  • Generates a .jar file, e.g., Hello.jar.

Running the Program

To run the compiled program, use the following command:

java -jar Hello.jar

What happens during execution?

The Java Virtual Machine (JVM) executes the compiled Kotlin bytecode:

  • Loads the Hello.jar file into memory.
  • Starts execution from the main function.
  • Prints Hello, World! to the console.
Hello, World!

You’ve now compiled and executed your first Kotlin program — a foundational step in learning the Kotlin programming language.

Common Issues to Watch For

  • Missing main function: Kotlin programs must have a main entry point.
  • Case sensitivity: Kotlin is case-sensitive — main and Main are different.
  • Runtime errors: Make sure the Kotlin runtime is included when compiling to a .jar.

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...