codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

3 / 68 topics
1Introduction to Kotlin2Kotlin Installation3Hello World Program4Variables and Data Types5Operators in Kotlin6Control Flow Statements7Functions in Kotlin8Strings and Templates9Collections in Kotlin10Null Safety
Tutorials/Kotlin/Hello World Program
🎯Kotlin

Hello World Program

Updated 2026-04-20
3 min read

Introduction

Welcome to your journey into the world of Kotlin, a statically typed programming language that runs on the Java Virtual Machine (JVM) and also compiles to JavaScript source code. Kotlin is designed to be fully interoperable with Java, making it an excellent choice for Android development and other JVM-based projects.

In this tutorial, we will create our first Kotlin program: the classic "Hello World" application. This simple program will print "Hello, World!" to the console, serving as a stepping stone to understanding Kotlin's syntax and structure.

Setting Up Your Environment

Before you start coding, ensure that your development environment is set up correctly. You can use any Integrated Development Environment (IDE) that supports Kotlin, such as IntelliJ IDEA or Android Studio. For this tutorial, we will assume you are using IntelliJ IDEA.

Step 1: Install IntelliJ IDEA

  1. Download and install the latest version of IntelliJ IDEA from JetBrains.
  2. During installation, select the "Kotlin" plugin to be installed automatically.

Step 2: Create a New Kotlin Project

  1. Open IntelliJ IDEA and select "Create New Project."
  2. In the project creation wizard, choose "Kotlin" under the "Other Languages" section.
  3. Click "Next" and configure your project settings:
    • Name: HelloWorld
    • Location: Choose a directory on your computer where you want to save the project.
  4. Click "Finish" to create the project.

Writing Your First Kotlin Program

Now that your environment is set up, let's write our first Kotlin program.

Step 1: Create a New Kotlin File

  1. In the Project Explorer (usually located on the left side of IntelliJ IDEA), right-click on the src directory.
  2. Select "New" > "Kotlin File/Class."
  3. Name your file Main.kt and click "OK."

Step 2: Write the Hello World Program

Open the newly created Main.kt file and replace its contents with the following code:

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

Explanation of the Code

  • fun main(): This is the entry point of a Kotlin application. The main function is where execution begins.
  • println("Hello, World!"): This line prints the string "Hello, World!" to the console. println is a standard library function that outputs text followed by a newline.

Running Your Program

To execute your program and see the output, follow these steps:

  1. In IntelliJ IDEA, click on the green play button (or press Shift + F10) located at the top of the editor window.
  2. Observe the console output in the "Run" tab below the editor. You should see "Hello, World!" printed.

Best Practices for Writing Kotlin Code

While writing your first program, it's essential to follow some best practices that will help you write cleaner and more maintainable code as you progress.

1. Use Meaningful Names

  • Variable Names: Choose names that clearly describe the purpose of the variable.
  • Function Names: Use verbs or verb phrases to indicate what a function does.

Example:

fun greet(name: String) {
    println("Hello, $name!")
}

2. Keep Functions Short and Focused

Each function should perform a single responsibility. If a function is doing too much, consider breaking it down into smaller functions.

Example:

fun printGreeting() {
    val name = "World"
    greet(name)
}

fun main() {
    printGreeting()
}

3. Use String Templates

Kotlin provides string templates that allow you to embed expressions inside strings using the $ symbol.

Example:

val greeting = "Hello, World!"
println(greeting)

4. Handle Exceptions Gracefully

Use try-catch blocks to handle potential exceptions and prevent your program from crashing unexpectedly.

Example:

fun divide(a: Int, b: Int): Double {
    return try {
        a.toDouble() / b
    } catch (e: ArithmeticException) {
        println("Error: Division by zero")
        0.0
    }
}

Conclusion

Congratulations! You have successfully created and run your first Kotlin program. The "Hello World" application is a simple yet powerful way to get started with Kotlin, providing you with the foundation to explore more complex features and concepts.

As you continue learning Kotlin, remember to practice regularly and build small projects to reinforce your understanding. Kotlin's rich ecosystem and strong community support will undoubtedly help you become proficient in this versatile programming language.

Happy coding!


PreviousKotlin InstallationNext Variables and Data Types

Recommended Gear

Kotlin InstallationVariables and Data Types