In this article, We will learn how to start with Kotlin Programming and will write a small print “Hello World” program.
Steps:
- IDE (Java Editor) Installation
- Writing Hello World Kotlin program.
Kotlin Editor
To write your Kotlin programs, you will need a text editor. There are different IDEs (Integrated Development Environment) available in the market like Eclipse, Android Studio or IntelliJ IDEA, which are particularly useful when managing larger collections of Kotlin files.
- Android Studio − An open-source and free IDE provided by Google which supports both Java and Kotlin language. It can be downloaded from https://developer.android.com/studio.
- Eclipse − Its Java IDE earlier but now it also supports Kotlin and can be downloaded from https://www.eclipse.org/.
- IntelliJ IDEA − An IDE developed by JetBrains which supports Java, Kotlin, Android, etc and it’s free and open-source which can be downloaded from https://www.jetbrains.com/idea/.
I will prefer to use IntelliJ IDEA Community version. Now go to the above-mentioned link to download IntelliJ IDEA and install it.
Creating New Project in IntelliJ IDEA
1 . After installation the IntelliJ platform looks like this:
2 . Click on Create new Project.
3 . In the New Project wizard, select Kotlin from the list on the left and also select JVM |IDEA. Click Next.
4 . Name the project, change the location of the project if you want, provide project SDK path and click ‘Finish‘.
5 . Inside the src folder, to create a new Kotlin file right-click and then select New then Kotlin File/Class.
6. Now named the class which you want In my case I will name it as HelloWorld.
7. Inside the HelloWorld.kt file, I will write the following code to print “Hello, World!” to the screen:
Kotlin “Hello, World!” Program
fun main(args: Array<String>) { println("Hello, World!") }
8 . Run the program using the Run button.
When you run the program, You can see the output of the program in the Console screen, the output will be:
Understanding Code:
Let’s understand how “Hello, World!” program works in Kotlin.
The main Method
1 . fun main(args: Array<String>){…}
This is the main function, which is mandatory in every Kotlin application. The Kotlin compiler starts executing the code from the main function.
The function takes array of strings as a parameter and returns Unit.
Unit is same as void in Java which means that this method doesn’t return any value.
The main method must have this exact signature in order to be able to run our program.
fun main(args: Array<String>) { ... }
Note: In Kotlin, to define a function we have to use fun keyword which tells the compiler that it’s a function.
2 . println(“Hello, World!”)
The println() function is an upgraded version of print() which prints any argument passed to it and adds a new line to the output.
In this program, it will print Hello, World! and add a new line.
Note: There is no need to end code statements with a semicolon( as we do in Java) because Kotlin performs this internally.
Comparison With Java “Hello, World!” program
To learn or compare how to write Hello World Program in Java refer this article.
1 . Unlike Java, it is not mandatory to create a class in every Kotlin program. It’s because the Kotlin compiler creates the class for us.
The name of the Kotlin class is (the name of the Kotlin file) + suffix (Kt). For example, corresponding to HelloWorld.kt file the generated class name is HelloWorldKt.
2. The println() function calls System.out.println() internally.
If you are using IntelliJ IDEA, put your mouse cursor next to println and press( Shortcut: Ctrl + B For Mac: Cmd + B), this will open Console.kt (declaration file).
In Console.kt file you can see that println() function is internally calling System.out.println().