Kotlin Tutorial

Kotlin Introduction

Kotlin is a modern, trending programming language that was released in 2016 by JetBrains.

It has become very popular since it is compatible with Java (one of the most popular programming languages out there), which means that Java code (and libraries) can be used in Kotlin programs.

Kotlin is used for:

  • Mobile applications (especially Android apps)
  • Web development
  • Server side applications
  • Data science
  • And much, much more!

Why Use Kotlin?

  • Kotlin is fully compatible with Java
  • Kotlin works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • Kotlin is concise and safe
  • Kotlin is easy to learn, especially if you already know Java
  • Kotlin is free to use
  • Big community/support

Kotlin Get Started

Kotlin IDE

The easiest way to get started with Kotlin, is to use an IDE.

An IDE (Integrated Development Environment) is used to edit and compile code.

In this chapter, we will use IntelliJ (developed by the same people that created Kotlin) which is free to download from https://www.jetbrains.com/idea/download/.


Kotlin Install

Once IntelliJ is downloaded and installed, click on the New Project button to get started with IntelliJ:

Then click on “Kotlin” in the left side menu, and enter a name for your project:

Next, we need to install something called JDK (Java Development Kit) to get our Kotlin project up and going. Click on the “Project JDK” menu, select “Download JDK” and select a version and vendor (e.g. AdoptOpenJDK 11) and click on the “Download” button:

When the JDK is downloaded and installed, choose it from the select menu and then click on the “Next” button and at last “Finish”:

Now we can start working with our Kotlin project. Do not worry about all of the different buttons and functions in IntelliJ. For now, just open the src (source) folder, and follow the same steps as in the image below, to create a kotlin file:

Select the “File” option and add a name to your Kotlin file, for example “Main”:

You have now created your first Kotlin file (Main.kt). Let’s add some Kotlin code to it, and run the program to see how it works. Inside the Main.kt file, add the following code:

Main.kt

fun main() {
  println("Hello World")
}

Don’t worry if you don’t understand the code above – we will discuss it in detail in later chapters. For now, lets focus on how to run the code. Click on the Run button at the top navigation bar, then click “Run”, and select “Mainkt”.

Next, IntelliJ will build your project, and run the Kotlin file. The output will look something like this:

As you can see, the output of the code was “Hello World”, meaning that you have now written and executed your first Kotlin program!

Kotlin Syntax

In the previous chapter, we created a Kotlin file called Main.kt, and we used the following code to print “Hello World” to the screen:

Example

fun main() {
  println("Hello World")
}

Example explained

The fun keyword is used to declare a function. A function is a block of code designed to perform a particular task. In the example above, it declares the main() function.

The main() function is something you will see in every Kotlin program. This function is used to execute code. Any code inside the main() function’s curly brackets {} will be executed.

For example, the println() function is inside the main() function, meaning that this will be executed. The println() function is used to output/print text, and in our example it will output “Hello World”.

Good To Know: In Kotlin, code statements do not have to end with a semicolon (;) (which is often required for other programming languages, such as JavaC++C#, etc.).


Main Parameters

Before Kotlin version 1.3, it was required to use the main() function with parameters, like: fun main(args : Array<String>). The example above had to be written like this to work:

Example

fun main(args : Array<String>) {
  println("Hello World")
}

Note: This is no longer required, and the program will run fine without it. However, it will not do any harm if you have been using it in the past, and will continue to use it.

Kotlin Output (Print Text)

Kotlin Output (Print)

The println() function is used to output values/print text:

Example

fun main() {
  println("Hello World")
}

You can add as many println() functions as you want. Note that it will add a new line for each function:

Example

fun main() {
  println("Hello World!")
  println("I am learning Kotlin.")
  println("It is awesome!")
}

You can also print numbers, and perform mathematical calculations:

Example

fun main() {
  println(3 + 3)
}

The print() function

There is also a print() function, which is similar to println(). The only difference is that it does not insert a new line at the end of the output:

Example

fun main() {
  print("Hello World! ")
  print("I am learning Kotlin. ")
  print("It is awesome!")
}

Note that we have added a space character to create a space between the sentences.

Continue………..

Scroll to Top