Saturday, January 5, 2019

Kotlin : The Next Generation Programming Language for Android.

Kotlin : The Next Generation Programming Language for Android.


Kotlin is relatively new programming language developed by JetBrains. The language has gained immense popularity in the developer's community on account of its features. It is an open source, general purpose, statically typed, Java compatible programming language, which runs like a charm on Java Virtual Machine. JetBrains has also developed intelliJ IDEA and JetBrains IDE. Android Studio is based on JetBrains IDE, this makes Kotlin an ideal choice for Android development. Only an year after the launch of Kotlin, Google announced its official support to Kotlin on Android and credited it to be a first-class language.

At first glance, Kotlin looks like a streamline version of Java, it is statically typed and object-oriented like Java but it offers more than what Java offers to developers. It has many new functional programming features, clean syntax and many other enhancements over Java.

Let's see what Kotlin offers to the developers -

  • Kotlin is Open Source - The language is open source and allows you to swiftly convert any existing Java code with a single click tool. 
  • Java Interoperable - The highlight of this language is that it is fully compatible with Java. It uses Java libraries, Java tools  and runs smoothly on Java Virtual Machine. It also offers backward compatibility for older versions of Java.
  • Kotlin Compiles to JVM Bytecode - Kotlin easily compiles JavaScript or JVM bytecode, JS and Java.
  • Kotlin Data Classes - In Java, a simple data class is loaded with excessive code which one needs to skip in order to find out the real purpose of that class. However, in Kotlin the same Java code can be written in a simple manner, thus saving loads of code typing exercise.

In Java, it is common practice to first create a model class with getters and setters, but in Kotlin you do not need getters and setters, you can simply add data in front of the class.

Let's see a model class in Java :

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Now, let's see the same code written in Kotlin :

data class Student(internal val name:String, internal val age:Int)

  • Kotlin is Null Safe - Kotlin has saved the developers from the perils of null references, commonly known as billion dollar mistake. In Java, if you try to access the member of null reference, it results in null reference exception. Whereas  Kotlin does not compile any code that is assigned or returns a null value. It first checks the assigned value, if it is not null only then it executes the operation.
  • Extention Functions - Kotlin allows you to add extra methods to classes without altering their source code. 


Take a look at the code given below, here an extra function is being added to an integer which multiplies any number by 5

fun Int.multi() = this * 5
print(50.multi())
This will print 250 as a result.

  • Kotlin  Requires Less Coding - In Java, you are supposed to write code for everything. Unlike Java, Kotlin complier understands the code and is capable of writing the remainig code. For example, Kotlin can infer different types in variable declarations. This feature helps developers in completing the task with less code, thus increasing productivity.
  • Kotlin is Free - Kotlin does not come with a charge.


Apart from the features mentioned above, Kotlin has fixed a series of issues that Java has been suffering from :

  • Null references are controlled by the type system.
  • No raw types in Kotlin
  • Arrays in Kotlin are invariant
  • As opposed to Java's SAM-conversions, Kotlin has proper function types
  • Use-site variance without wildcards
  • Kotlin does not have checked exceptions


In short, Kotlin has made Android development more fun, it enables you to write more expressive and efficient code with lesser bugs.

No comments:

Post a Comment