Kotlin

Get Started with Kotlin Programming Language

In order to Get Started with Kotlin, first, you need to install the latest version of IntelliJ IDEA. So, choose the Community download and save the exe file. After that, click on the exe file that you have just downloaded and follow the installation instruction.

Downloading the IntelliJ IDEA IDE
Downloading the IntelliJ IDEA IDE

Once, it is installed, you can open the IntelliJ IDEA. The following interface will be shown to you. Select New Project.

Creating New Project in IntelliJ IDEA
Creating New Project in IntelliJ IDEA

For the purpose of creating a basic application in kotlin, select kotlin from the left sidebar and choose Console Application from the right. Also, provide a suitable name to the application.

Create Kotlin Console Application

After that click on the Next button and then click on Finish.

Kotlin Console Application
Kotlin Console Application

Now that you have got the project workspace opened, you can start coding there. Initially, you can open the Main.kt file to write the code.

Creating a Basic Application to Get Started with Kotlin Programming Language
Creating a Basic Application to Get Started with Kotlin Programming Language

As shown above, select Main.kt. It will open this file where you can find the main() function. Further, the println() method displays output on the console. So, here you can provide a text string that you want to display as output. Finally, you can build, and run the application. The following figure demonstrates the output.

Output

A Simple Hello World Program in Kotlin
A Simple Hello World Program in Kotlin

Compute Sum of Two Numbers in Kotlin

Another simplest example is the sum of two numbers. The following code demonstrates it. Basically, the following example shows how variables are declared and initialized. Also, it shows the simple computation expression, and a string made of literals as well as varibles that is passed to the println() function as parameter.

fun main(args: Array<String>) {
    val a: Int=12
    val b: Int=78
    val c : Int=a+b;

    println("Sum of $a and $b is $c")
}

As shown above, the variables are declared using the val keyword. In fact, we have another keyword in kotlin to declare variables – var. While, the variables declared using val are immutable. In contrast, variables declared using var are mutable and can be changed.

Output

Sum of 12 and 78 is 90


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *