All CategoriesAndroid App Development

Introduction to Kotlin Programming Language 2017

Kotlin is a statically typed programming language which works on multiple platforms. It is a new programming language that runs on JVM and can also be compiled into JavaScript. Also, it is useful for Android as it is compiled into JVM bytecode.
Google officially announced that Kotlin is a supported language for Android on 17th May 2017. Kotlin was developed by JetBrains, the same team which developed IntelliJ and Android Studio. Its latest version is 1.1.2 Its name comes from Kotlin Island near St. Petersberg, Russia where JetBrains developers did the primary development.
There is a plugin from JetBrains which is available on the official website. We can easily install it and build the applications on Java and Android in this new language.
Kotlin is 100% interoperable with Java and Android and compatible with Java, which means we can easily convert the Java code into Kotlin code and vice-versa also, it can use any frameworks and Library in Java.
In Android, it is integrated with Gradle, after successful installation of the Kotlin plugin in the Android Studio.

Why Use Kotlin?

As the programming world moves ahead, the programming languages get obsoleted and cannot compete with the modern languages. As we see in Android, below Android 7.1, developers are using Java 6 or some parts of Java 7 and above Android 7.1 they are using Java 8 functionalities, which is not quite new. The developers consider it as a simple language which supports Android just as “iOS supports Swift,” which makes coding easy and simple.
Kotlin comes with some features like it is concise and reduces the amount of boilerplate code, safe as it avoids the entire class errors like null pointer exceptions, interoperable as it supports the existing libraries for JVM, Android and the browser, Tool-Friendly as one can choose any Java IDE or build from the command line.
Kotlin is an open source, and its support for Android Studio is gradually improving. The developer may easily integrate with existing Java project and Android project. It is a lightweight programming language and it has only 7000 methods as compared to the Java libraries.
As discussed above, it has new features which make the language simple and safe, supporting lambdas, null safety, extensions, data classes, optional arguments, etc.

New Features of Kotlin

Kotlin comes with a new set of concepts which makes this language more readable, safe and clean. Let’s see some features that it addresses that used to be previously mentioned as Java limitations.
Null Safety:
A variable cannot be null. We have to add “?” this operator in an expression to make the variable nullable. Here, it doesn’t give the Null pointer exception just come with the null result. We can safely call the methods on nullable variables.
val x: Int = null // compile error
val y: Int? = null // ok
Lambdas:
The Lambdas can be used as
button.setOnClickListener({ view -> Log.d(“Kotlin”,“Click”)})
Data Class:
The data class can be used as
data class City(val name: String, val state: String)
There is no need to generate the setters, getters separately or use some implementations like equals(), toString(), hashcode(), etc.

Java vs Kotlin

Java is one of the most widely used programming languages and it is the official language for Android development. But as I discussed above, there are some reasons that it is not always the best option for Android development.
Let’s see some differences of code in Java and Kotlin:
An Example of Data Class in Java & Kotlin:
In Java:

public class Station {
    private String id;
    private String address;
    private String city;
    private double latitude;
    private double longitude;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * And many more getters and setters
     */
}

In Kotlin:
data class Station(val id: String, val address: String, val city: String, val latitude: Double, val longitude: Double)
Here, we can see very well that there is no need to generate the getters, setters and write the implementations of toString(), equals() methods, etc.
An example of How to Handle Collections in Java and Kotlin:
In Java:

void processList(List<Integer> list) {
        List<String> stringList = new ArrayList<>();
        for (Integer i : list) {
        if (i % 2 == 0) {
        stringList.add(i.toString());
        }
        }
        }

In Kotlin:

fun processList(list: List<Int>) {
        val stringList = list.filter { it % 2 == 0 }. map { it.toString() }
        }
        }

Here, we can reduce the complexity and multiline instructions of code.

Why isn’t Kotlin so Popular?

The first release of Kotlin is Version 1.0 in Feb 2017 and we see this language is still developing with many existing drawbacks. Many people also complain that the build time is longer in comparison with pure Java. The final APK file takes much MB. These are a few of the drawbacks, but the language is still evolving and in the upcoming days it has the chance of becoming one of the most popular language.

Conclusion:

As discussed above, Kotlin is one of the best option, easy to learn, comes with low risks, and we can easily start it alongside with Java. Google has made it official and open source for Android. And its features are fully compatible with Java. We can easily change the code from Java into Kotlin or vice versa, as in Android also we can change the code by installing it plugin. As discussed it runs on JVM, and it compiles into JavaScript and is also useful for Android that compiles into JVM bytecode. As a new language, it is safe and expressive though it has a few disadvantages. It is getting enhanced day by day and we can conclude by saying that it is evolving as a favorite language for an Android developer.
Enroll for Android Developer Training conducted by Acadgild and become a successful Android developer.

Tags

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close