🚀 Kotlin – Get Started: Hello World & Your First Kotlin Program
🧲 Introduction – Starting Your Kotlin Journey
Ready to write your first lines of Kotlin? Whether you’re a beginner or transitioning from Java, starting with a simple Kotlin program like “Hello World” is the best way to understand the syntax, compiler behavior, and IDE workflow. Kotlin’s clear and concise structure makes it beginner-friendly and highly productive.
🎯 In this guide, you’ll learn:
- How to write and run your first Kotlin program
- The structure of a basic Kotlin file
- Differences between using Kotlin in CLI, IDEs, or online editors
- How the Kotlin
main()
function works
💻 Your First Kotlin Program – “Hello, World!”
Let’s begin with the classic starting point in every language: printing “Hello, World!” to the console.
✨ Kotlin Hello World Example
fun main() {
println("Hello, World!")
}
🔍 Code Breakdown:
Line | Explanation |
---|---|
fun main() | Declares the main function – the program’s entry point. |
{ ... } | The code block that holds executable statements. |
println() | Standard Kotlin function to print output to the console. |
✅ Output:
Hello, World!
🧰 How to Run Your First Kotlin Program
🟢 Option 1: Using IntelliJ IDEA
- Open IntelliJ ➝ File ➝ New Project ➝ Kotlin ➝ Kotlin/JVM.
- Create a file:
Main.kt
- Paste the Hello World code.
- Click Run ▶️ or right-click → Run ‘MainKt’
🟠 Option 2: Using Command Line (CLI)
- Install Kotlin compiler CLI via SDKMAN or package manager.
- Save code as
hello.kt
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar
✅ You should see: Hello, World!
🌐 Option 3: Using Kotlin Playground (Online)
- Visit https://play.kotlinlang.org
- Paste the code and click Run
⚡ Instant output in your browser without setup.
🧪 Writing a Slightly Advanced Program
Let’s modify the program to accept input and greet the user.
fun main() {
print("Enter your name: ")
val name = readLine()
println("Hello, $name! Welcome to Kotlin.")
}
🔍 Code Explanation:
print()
– Prints without newline.readLine()
– Reads user input from the console.$name
– String template to embed variable inside text.
✅ Sample Output:
Enter your name: Arjun
Hello, Arjun! Welcome to Kotlin.
📘 Structure of a Kotlin File
Kotlin Element | Example | Purpose |
---|---|---|
fun main() | Entry point | Starts the program |
val / var | val name = "Kotlin" | Declares variables (immutable/mutable) |
println() | println("Hi") | Outputs text to console |
Comments | // single-line / /* */ | Document your code |
Blocks | { ... } | Code grouping and scoping |
⚙️ Best Practices for Getting Started
Practice | Tip |
---|---|
✅ Use val by default | Promotes immutability unless change is needed |
🔍 Use readLine() carefully | It returns nullable String (String? ) |
📂 Organize Kotlin files | Keep .kt files inside src directory in IDE |
🚫 Avoid unnecessary semicolons | Kotlin does not require ; at line ends |
🧠 Understand main() | It’s essential for CLI apps and learning syntax |
📌 Summary – Recap & Next Steps
You’ve just written your first Kotlin program and learned the structure of a basic Kotlin app. With just a few lines of code, Kotlin shows off its simplicity, readability, and developer-friendliness.
🔍 Key Takeaways:
- The
main()
function is the entry point for Kotlin applications. - Use
println()
for output andreadLine()
for input. - You can run Kotlin using IDEs, CLI, or online playgrounds.
⚙️ Practical Use:
From printing messages to taking input, Kotlin makes it easy to start writing programs—ideal for Android apps, backend systems, or scripting tasks.
❓ FAQs – Kotlin First Program
❓ What is the main function in Kotlin?
✅ The main()
function is the entry point for every Kotlin program. It’s where execution begins.
❓ Do I need a class to run Kotlin code?
✅ No. Unlike Java, Kotlin doesn’t require wrapping your code inside a class for a basic program.
❓ How do I run Kotlin code in a browser?
✅ Use Kotlin Playground to write and execute code instantly online.
❓ Can Kotlin be run without IntelliJ?
✅ Yes. You can compile and run Kotlin from the command line using the kotlinc
compiler and Java runtime.
❓ What’s the difference between print()
and println()
in Kotlin?
✅ print()
doesn’t add a newline at the end, while println()
adds a newline after printing the output.
Share Now :