Andre Ikeda Lima | Android Especialist | everis Brazil

Kotlin, An Overview of the New Language for Android

By 2009, I slowly started my path towards developing mobile applications. Even in the days of J2ME, and then after Java Android, I felt the need for a less wordy language, one that didn’t have such a steep learning curve (especially in the case of J2ME). It was a long road before Kotlin came into my life, but even today, it is definitely the best language that I have ever run into.

It was 2010 when a group of JetBrains engineers initiated the development of the Kotlin language with the intention of meeting the shortfalls of a company that had its origins in other languages, with the exception of the Scala language. However, Scala’s build time has always been an obvious drawback.

In February of 2016, the language was released under the alias of Kotlin v1.0, which was the first official and stable release. A little more than a year after the launch, Google announced, at the Google I/O event, the official language support for Android.

When I started to study Kotlin, I had some notion of the difficulties that might arise along the way, but I was surprised with the language’s fast learning curve and, in this article, I will show some of the knowledge I have acquired.

Interoperability with Java

To start off, Kotlin is 100% interoperable with Java, which means it is possible to have the two languages in the same project. Another great advantage is the Android Studio plugin that converts Java code to its corresponding form in Kotlin.

Figure 1. In the Code menu, you see the option to convert Java code into Kotlin.

Simplicity

For veterans who have programmed wordy languages like Java for years and have naturally handled the many lines of code to perform functions, they are used to it and think it’s simple. However, when we put the two languages​​ (Java/Kotlin) side by side, we see how much more a Java programmer has to type for an application to perform the same function.

Figure 2. Creating a template for a NavigationDrawerActivity in Java using the OnCreate method
Figure 3. Creating a template for a NavigationDrawerActivity in Kotlin using the OnCreate method

As you can see in Figures 3 and 4, Kotlin has a more concise programming dynamic, which makes it easier to understand.

Null-safety

One of the biggest problems in the Java language, if not the largest, are the NullPointerExceptions, affectionately called NPE. In Java, for each access to an object, the programmer should check if it’s null. On the other hand, Kotlin offers a more efficient way to make this verification because the language itself distinguishes nullable references from non-null references.

Figure 4. Declaration of a non-nullable reference type.
Figure 5. For the variable to be null, simply declare it with an “?” at the end.

In Figure 4, there is a declaration of a variable that can’t be null. When we try to declare this variable as null, the compiler informs us that this action is unfeasible. However, in Figure 5, we already declare the variable with the possibility of being null. In this scenario, when we declare the variable to be null, the application is normally compiled.

Extensions

One of the most interesting features Kotlin has is its use of extensions for adding new methods to a class without the need to inherit the class itself; this includes implementing all methods. What is the motivation behind this approach? The answer is simple: avoid Java’s wordiness.

Figure 6. Snippet of code for changing the largest element in each list.
Figure 7. Extension of the method for changing elements in a list.
Figure 8. The same snippet of code in Figure 6, but in Kotlin.

You can find out more about Kotlin’s advantages and limitations compared to Java on their website. I finish by saying that the use of Kotlin as the main language in developing Android applications has not only brought me new inspiration in the sense of learning, but also has made my life as a developer simpler and less wordy.

By: André de Lima

Exponential intelligence for exponential companies