Creating a Hello world program with kotlin and IntelliJ Idea.
In the below tutorial, we’re going to write a simple Hello World script in Kotlin. We will get familiar with creating kotlin files and basic syntaxes in further posts.
- This tutorial assumes, you have a basic working knowledge of IntelliJ Idea.
- Have Kotlin installed setup in your machine.
Open up IntelliJ Idea and Create a new Kotlin project with a JVM target based on the IntelliJ IDEA build system.
Create a new Kotlin class file, named “Helloworld” under src folder.
For executing code in Kotlin, we need to have a main function. This main function is called upon when the file is being executed, just like Java’s main function. The syntax for main function is :
For printing, we call println(“String”) method, which prints the passed string arguement and a new line after that.
And unlike java, we don’t have to provide semi colons after each line.
And that’s it. Our Hello world program is ready to be executed. Now we can execute Kotlin script from within IntelliJ by clicking run. But to understand how Kotlin works, we will go ahead and execute it via command line.
The first line, $ kotlinc Helloworld.kt compiles the kotlin file to class. The second line executes the created class file.
And with this we have completed our first program in Kotlin. :)
Keep coding! Have fun.