Hello World in Kotlin
Kotlin is a modern, statically-typed programming language that is fully interoperable with Java. It's an official language for Android development and is widely adopted by companies and developers all over the world. One of the great things about Kotlin is its simplicity and readability, making it an excellent choice for beginners. In this article, we will learn how to write a simple "Hello, World!" program in Kotlin.
What you'll need
Before we start, you need to have Kotlin installed on your machine. Kotlin programs can be developed using any text editor, but for the sake of this tutorial, we will use IntelliJ IDEA, a popular Integrated Development Environment (IDE) for Kotlin.
Let's get started
Once you have your IDE ready, let's get started by creating a new Kotlin project:
- Open IntelliJ IDEA and click on
Create New Project
. - Choose
Kotlin
on the left-hand panel andKotlin/JVM
on the right. - Name your project and click
Finish
.
Now that your project is created, let's create a new Kotlin file:
- Right-click on the
src
directory and chooseNew -> Kotlin Class/File
. - Name it
Main
and chooseFile
.
Now you should have a new file called Main.kt
. This is where you'll write your Kotlin code.
Writing the code
In your Main.kt
file, type the following code:
fun main() {
println("Hello, World!")
}
Let's break this down:
fun main() {}
is the main function. This is the entry point of your Kotlin program. When you run your program, the code inside the main function is executed.println()
is a built-in Kotlin function that prints the specified message to the standard output (like your console or terminal), followed by a new line."Hello, World!"
is the message we want to print. In Kotlin, text values are represented with strings, and strings are enclosed in double quotes.
Running the program
To run your program, right-click anywhere in your code and choose Run 'MainKt'
. You should see Hello, World!
printed in the output console.
Congratulations, you've just written and run your first Kotlin program!
Wrapping up
In this tutorial, you've learned how to write a simple Kotlin program that prints "Hello, World!" to the console. You've also learned about the main function, the println()
function, and strings in Kotlin.
Remember, learning to code is a gradual process, so don't rush. Take your time to understand the basics before moving on to more complex topics. Keep practicing, and happy coding!