Audience

This tutorial has been prepared for the beginners to help them understand the basics of Kotlin programming language.

Prerequisites

Before proceeding with this tutorial you should have a basic understanding of Java programming language. Although it is a beginners’ tutorial, I assume that the readers have a reasonable exposure to any programming environment and knowledge of basic concepts such as variables, commands, syntax, etc. I strongly recommend that you gain some basic knowledge of Java programming language before proceeding with Kotlin programming.

Introduction

Kotlin is influenced by other programming languages such as Java, Scala, Groovy, Gosu, etc. The syntax of Kotlin may not be exactly similar to JAVA, however, internally Kotlin is reliant on the existing Java Class library to produce wonderful results for the programmers. Kotlin provides interoperability, code safety, and clarity to the developers around the world.

Kotlin is influenced by other programming languages such as Java, Scala, Groovy, Gosu, etc. The syntax of Kotlin may not be exactly similar to JAVA, however, internally Kotlin is reliant on the existing Java Class library to produce wonderful results for the programmers. Kotlin provides interoperability, code safety, and clarity to the developers around the world.

Advantages and Disadvantages

Following are some of the advantages of using Kotlin for your application development.

Easy Language
Kotlin is a functional language and very easy to learn. The syntax is pretty much similar to Java, hence it is very easy to remember. Kotlin is more expressive, which makes your code more readable and understandable.
Concise
Kotlin is based on JVM and it is a functional language. Thus, it reduce lots of boiler plate code used in other programming languages.
Runtime and Performance
Better performance and small runtime.
Interoperability
Kotlin is mature enough to build an interoperable application in a less complex manner.
Brand New
Kotlin is a brand new language that gives developers a fresh start. It is not a replacement of Java, though it is developed over JVM. It is accepted as the first official language of android development. Kotlin can be defined as - Kotlin = JAVA + extra updated new features.

Following are some of the disadvantages of Kotlin.

Namespace declaration
Kotlin allows developers to declare the functions at the top level. However, whenever the same function is declared in many places of your application, then it is hard to understand which function is being called.
No Static Declaration
Kotlin does not have usual static handling modifier like Java, which can cause some problem to the conventional Java developer.
Environment Setup

However, if you still want to use Kotlin offline in your local system, then you need to execute the following steps to configure your local workspace.

  1. Java 8 installation
  2. Kotlin runs on JVM, hence. it is really necessary to use JDK 8 for your local Kotlin development.

  3. IDE installation.
  4. There are a number of IDE available over the internet. Like: NetBeans, Eclipse, Intellij

  5. Configure your IDE
  6. Install Kotlin development plugins in your IDE.

  7. Create project & test
  8. When your development environment is ready, create a Kotlin project. Add the following piece of code in the “project_name.kt” file.

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

    Run it as a Kotlin application and see the output in the console as shown in the following screenshot.

    Hello, World!
Architecture

Kotlin is a programming language and has its own architecture to allocate memory and produce a quality output to the end user. Following are the different scenarios where Kotlin compiler will work differently, whenever it is targeting different other kind of languages such as Java and JavaScript.

Kotlin compiler creates a byte code and that byte code can run on the JVM, which is exactly equal to the byte code generated by the Java .class file. Whenever two byte coded file runs on the JVM, they can communicate with each other and this is how an interoperable feature is established in Kotlin for Java.

Whenever Kotlin targets JavaScript, the Kotlin compiler converts the .kt file into ES5.1 and generates a compatible code for JavaScript. Kotlin compiler is capable of creating platform basis compatible codes via LLVM.

Basic Types

In this chapter, we will learn about the basic data types available in Kotlin programming language.

  • Numbers
  • Characters
  • Boolean
  • Strings
  • Arrays
  • Collections
  • Ranges
Control Flow

There are different types of control flow mechanism available in the Kotlin. Like:

  • If-Else
  • For Loop
  • While Loop and Do-While Loop
  • Use of Return, Break, Continue, When

We will discuss in detail about For loop.

The implementation and use of For loop is conceptually similar to Java for loop. The following example shows how we can use the same in real-life examples.

fun main(args: Array) {
    val items = listOf(1, 2, 3, 4)
    for (i in items) println("values of the array"+i)
}

In the above piece of code, we have declared one list named as “items” and using for loop we are iterating through that defined list and printing its value in the browser. Following is the output.

values of the array1
values of the array2
values of the array3
>values of the array4

Following is another example of code, where we are using some library function to make our development work easier than ever before.

fun main(args: Array) {
    val items = listOf(1, 22, 83, 4)

    for ((index, value) in items.withIndex()) {
        println("the element at $index is $value")
    }
}

Once we compile and execute the above piece of code in our coding ground, it will yield the following output in the browser.

the element at 0 is 1
the element at 1 is 22
the element at 2 is 83
the element at 3 is 4

Functions

Kotlin is a statically typed language, hence, functions play a great role in it. We are pretty familiar with function, as we are using function throughout the examples. Function is declared with the keyword “fun”. Like any other OOP, it also needs a return type and an option argument list.

In the following example, we are defining a function called MyFunction and from the main function we are calling this function and passing some argument.

fun main(args: Array) {
    println(MyFunction("This Tutorial"))
}br> fun MyFunction(x: String): String {
    var c:String = "Hey!! Welcome To "
    return (c+x)
}

The above piece of code will yield the following output in the browser.

Hey!! Welcome To This Tutorial

Some of the different types of function available in Kotlin are as follows:

  • Lambda Function
  • Inline Function
Exception Handling

Exception handling is a very important part of a programming language. This technique restricts our application from generating the wrong output at runtime. In this chapter, we will learn how to handle runtime exception in Kotlin. The exceptions in Kotlin is pretty similar to the exceptions in Java. All the exceptions are descendants of the “Throwable” class.

Note: Like Java, Kotlin also executes the finally block after executing the catch block.

Reference
  • All the documentation in this page is taken from Tutorials Point Kotlin Quick Guide. I do not own any material all documents are copied from site just for learning purpose.

Visit tutorialspoint.com to see this complete guide as well as thousand other resources for Online Education.