Write your first program in Kotlin

by keshav


Hello World.png

"Hello, World!" Program

 

Before we dive deep into Kotlin, let’s begin with a simple program that can print Hello world! In console. “Hello World!” printing program is a simple program that is often used to introduce a new programming language.

 

Let’s begin

 

1. Open IntelliJ IDEA and click on New Project

 

 

2. Select Kotlin from the list, and choose the required version of JDK. If you haven’t downloaded and installed JDK yet, you can review Installation of JDK. If you have downloaded and installed already but your installed JDK is not showing in the list, click on “add JDK” and select installed location, and click OK.

 

 

3. Write an appropriate name for your project and click on finish.

 

 

4. Now your project is created. You will see a window somewhat like as shown in the picture below. main.kt will be automatically loaded. If it was not loaded automatically you can locate the file and open it. The location is as src=>main=>Kotlin=>main.kt.

You will find the following code written. If not you can write it on your own. This code will print Hello World!. To run the code right-click in any blank area inside your code and click on run.

fun main(args: Array) {
    println("Hello World!")
}

 

 

5. Now we get the output of the program in the console. The output is “Hello World!”

 

 

 

At this point, you should have good information about IntelliJ IDEA and the process of writing and executing code in it. We wrote a code and executed it, but till now we haven’t seen the concept behind the program. Let’s understand the concepts and keywords used in Hello World Program.

fun main(args: Array) {
    println("Hello World!")
}

 

The first line in the program defines a function main (). It is the entry point of the program in kotlin. It is the first function that is called during the execution of the Kotlin program. In Kotlin, function means the group of statements that are grouped to perform a specific task. The definition of function starts with the keyword The main() function takes an array of string as a parameter and returns Unit (i.e. does not return any value). If you are familiar with java, then the Unit is the same as the void in java. Declaring Unit is optional.

 

The second line prints the string “Hello World!”. The function println() prints the given message inside the quotation marks and moves the cursor to the new line.

 


No Comments


Post a Comment