Kotlin program to print hello world:
For any programming language, the first program we learn is how to print the hello world string. You can use any IDE to write this program. This is a simple program that will print the “Hello World” string on the console. I will show you how to write it with IntelliJ-Idea in this post.
1. Create a new project:
Download IntelliJ-Idea from jetbrains website and also make sure that Kotlin is installed on your system. I am not going into detail about the installation process as it is different for different operating systems.
Open IntelliJ-Idea and click on File→New→Project. It will show you a list of inputs.
- Enter the Name of the project. You can give any name.
- Choose the location to create the project. You can also tick the Create Git Repository checkbox to make the project folder also a git repository.
- Select the Language as Kotlin.
- Select the build system, I am choosing Gradle for this example.
- Select the JDK and Gradle DSL.
- The last two settings are used to add a GroupId for the project and an ArtifactId.
Click on Create to create the project.
2. Create a Kotlin file to write the program:
Once the project is created, it will start the project. The src/main/kotlin folder will be empty when it is started. To create a new Kotlin file to write the program, right click on the folder → Select new → Select Kotlin Class/File.
Enter a name for the file and select File from the dropdown options. We can create different types of files like class, interface, data class etc. In this example, we are creating a File and the name of the file is ExampleTest.
3. Write the Hello World program:
It will create a new file ExampleTest.kt. Open the file and write the below program in that file:
fun main(args: Array<String>) {
println("Hello World!!")
}
We added a main function with a statement to print the Hello World!! string. The main function is the entry point of the program.
4. Run the program:
To run the program, click on the run icon in the gutter and click on Run ‘ExampleTestKt’.
It will build and run the program and the output will be displayed in a run tool window.
You can also press ⌃ ⇧ R
or the run button on the top right corner of the window to run the project.
You might also like:
- 6 ways to add items to a list in Kotlin
- Different ways to write to a file in Kotlin
- Kotlin program to print a multiplication table
- Kotlin program to convert a list to Array
- How to convert a stack trace to string in Kotlin
- 4 different Kotlin programs to add two numbers
- 2 ways in Kotlin to check if a year is a leap year or not