Mobile technology is one of the most disruptive innovations in the world today. Google’s Android and Apple’s iOS, are the operating systems used primarily in mobile technology, such as smart-phones and tablets. For almost two decades, Java was the prime language to build Android applications. This language was ruling the world of Android until the birth of a new language Kotlin. A comparative study of the two languages as described in this article will help you make the right decision and choose a language for your Android application development. So, will it be Java or Kotlin?
Before we jump deep into Kotlin here are few facts about Android and Java.
Android is an open source mobile phone operating system (OS) launched by Google. The OS is based on Linux kernel, which is written mostly in C. OS has to talk to the hardware and since all drivers are written in C/C++, Android is also written in C/C++. Android applications can be written in C/C++ however the preferred language is Java because Java is easier to learn and code than C++.
Java is a programming language first released by Sun Microsystems in 1995. Java now belongs to Oracle as Sun was bought over by Oracle. Java is a class-based Object Oriented Programming language. Internally, the Java application when compiled gets converted to Java bytecode which can run on any Java Virtual Machine (JVM) independent of the architecture. In Android, the bytecode gets executed on the Virtual Machine built in it. Java grew popular among developers because it was easy to code as compared to C++. it is object-oriented, robust, secure, portable and it has a high performance.
On the other hand, the rise of Kotlin has triggered the adoption among an eager group of Java developers.
Kotlin is powerful language primarily developed by JetBrains. It runs on JVM (Java Virtual Machine) and is statically typed programming language (statically-typed languages require you to declare the data types of your variables before you use them. Google has announced that Kotlin is the official programming language for Android. Kotlin was released in Feb 2016.
Both Java and Kotlin can be used to build Android applications and if one knows Java it is easy to learn Kotlin.
Here are a few similarities and comparison features in the two languages which will help developers and the management teams choose between the two.
1. Easy to Code
Since Kotlin is a new language and it was created from scratch the limitations in functions and features in Java have been resolved in Kotlin immensely. Kotlin is said to be a concise language as it has a variety of in-built functions to execute a block of code. As a result, it improves a developers’ productivity. The code written in Kotlin looks neat hence the maintainability and readability are effective and efficient.
Java being an old language including a new feature is cumbersome as every launch has to support the features of the previous ones along with the new feature. Hence coding in Java though is simpler than C++ it is now relatively complex as compared to Kotlin.
When it comes to the compilation, Java may be faster by 2%, however, this is only for full builds. Speed depends on the implementation of the language. If the new features and functions in Kotlin are understood and used appropriately then Kotlin is definitely a booster.
3. Exception handling
A very common exception that java developers face is the Null Pointer Exception(NPE). Debugging an NPE exception can kill time. Hence Java is an almost statically typed language, unlike Kotlin. A variable of type String in Java is not guaranteed to refer to a String— it might refer to null. Though Java developers are used to this, it negates the safety of static type checking, and as a result, Java developers have to live in constant fear of NPEs.
Kotlin resolves this by distinguishing between non-null types and nullable types. Types are non-null by default and can be made nullable if need be.
4. Memory usage
Kotlin runs on the same JVM as Java does, so you get the same Garbage Collection feature. It is also 100% compatible with Java libraries, so an object created with Kotlin is going to occupy the same amount of space as the same object created in Java. Hence clearing up of memory works in the same way for both the languages
5. Full Java interoperability
Kotlin classes can inherit from Java classes, implement Java interfaces, call Java methods, and so on. Conversely, Java code can inherit from Kotlin classes, implement Kotlin interfaces and call Kotlin methods. This means, if you already have a Java application it can be migrated to a Kotlin application. There may be a reduction in the lines of code in the final Kotlin version.
There are lots of other new features in Kotlin that could be a developer’s delight e.g. Usage of the Extension function, Lazy Loading, Inline functions, Receiver functions and more.
Kotlin is a new language hence resources with this skill are scarce. As a result, hands-on support to resolve bugs is not as easily available in Kotlin as for Java. On the other side, there is a huge community of Java programmers who can provide relevant answers to all critical bugs and coding problems in Java.
7. Cost Saving
We have already seen that Kotlin is easy to code. Less code implies fewer bugs and lesser coding time. This has a direct implication on cost saving as compared to Java.
So, Will, it be Kotlin or Java?
To summarize, Kotlin is an enhancement to Java and not a replacement. If you already have a group of experienced Java developers you could still continue with Java. If the developers do not know both then investing their time in learning and implementing Kotlin is worth it, for the simple reason that Kotlin has all the features that Java has and much more.