KOTLIN vs JAVA : Which one is best for Android Development ?
Some highlights about Kotlin !
At the latest Google I/O 2017, the Android team announced first-class support for Kotlin, a statically typed programming language for the JVM (Java Virtual Machine).
In fact, Kotlin has already became a buzz in the Android development for years, and more and more projects in Github have been developed by Kotlin. However, this language was always in a very awkward position for an Androider because it had never been supported officially. But now, the status quo has changed.
Main features of Kotlin (Compared with Java)
Full Java Interoperability
This is one of the best features of Kotlin programming language. Kotlin is 100% interoperable with Java, allowing to use all existing Android libraries in a Kotlin application. This includes annotation processing, so databinding and Dagger work too. Therefore, it is easy to learn for a Android developer who used to develop apps with Java, and it ensures the compatibility to existing Android projects.
Data Classes
In the Java world, a typical data class has tons of boilerplate code one needs to skip while finding out the real use of that class, such as the getters and setters. But in Kotlin, we can easily create a POJO (Plain Old Java Object) with getters, setters, equals()
, hashCode()
, toString()
and copy()
in a single line:
Therefore, if a class has a parameter list in its constructor, we must pass the corresponding parameter value when creating a new object (with Java, we can also create a non-parameter object and then assign values).
To create new objects, we don’t need the new
keyword anymore. And also, ;
can be omitted!
Java:
Safety
Kotlin provides built-in support for many things that are handled in Java these days with annotation processors — override checking, nullability analysis, etc. It also has safer numeric conversion rules.
I believe every Android or Java developer has met at least once the annoying NullPointerException, which is often called The Billion Dollar Mistake and named top1 of the top 5 crashes on Android. But Kotlin programming language’s type system aims at eliminating the perils of null references from code. For example, in Java, accessing a member of null reference results in a null reference exception. Kotlin doesn’t compile code that assigns or returns a null. This is seen as one of the most important features of Kotlin.
Get rid of those pesky NullPointer Exceptions:
Kotlin protects you from mistakenly operating on nullable types:
And if you check a type is right, the compiler will auto-cast it for you:
Extension functions
Thanks to the extension functions in Kotlin, you can add methods to classes without making changes to their source code. This feature applies to extend system Classes particularly, because if we want to extend the custom Class, there is no difference between “Extension” and “Adding method”.
Let’s look at an example:
Through these lines of code, we add a method printStr()
for the System class String, which is impossible in Java. Because we can only add methods by creating a new Class using inheritance in Java.
Less code
Because “developers are lazy”, most occurrences of a new language or concept are made to make developers’ life easier. The popularity of RxJava/RxAndroid in Android app development proved the efficiency of functional programming. However, Kotlin supports “Lambdas” naturally. Although from Java8, it has already been supported officially, but for such an OOP (Object-oriented programming) language, it’s still a little bit weird.
Let’s look at a snippet of code for clicking button to display text. In Java, it should be typically like this:
But in Kotlin, after import kotlinx.android.synthetic.main.<layout>.*
, we can easily use it like the code below:
We did the same logic with the less code!
Will Kotlin replace Java for Android app development?
There is a lot of arguments online after Kotlin was released. Some people said Kotlin will totally replace Java in the Android native app development. In my point of view, NO, even though I really prefer it and I like the way it does. But I think it won’t totally replace Java for Android app development.
Firstly, after having announced to make Kotlin a first-class language for writing Android apps, Google noted in a later keynote that this is only an additional language, not a replacement for its existing Java support.
Secondly, let’s review the official blog of Kotlin support on Android.
Today, at the Google I/O keynote, the Android team announced first-class support for Kotlin.
The keyword is “first-class”. What should we correctly understand this word? I think it just means Kotlin became one of the top-class language support for Android (so theoretically, at least, Kotlin should not overtop Java on Android app development).
Thirdly, the code base of Android Framework Layer is developed by Java. It is nearly impossible for Android team to port the whole framework on Kotlin. In the meantime, Java interacts with C/C++ through JNI (Java Native Interface), and Kotlin does the same way by far.
Lastly, even Kotlin has the ability or shows the trending of replacing Java, it should be a long long way to go. But it’s always not bad for our developers to learn new interesting technologies, let alone such an excellent programming language like Kotlin.
Kotlin at Wassa?
We have not integrated Kotlin in our Android projects yet, but we are willing to learn and try this magic partially in our next projects. It should be practical to simplify our development and to improve our code quality.
Do you want to know more about Wassa?
Wassa is an innovative digital agency expert in Indoor Location and Computer Vision.
Wether you are looking to help your customers to find their way in a building, enhance the user experience of your products, collect data from your customers or analyze the human traffic and behavior in a location, our Innovation Lab brings scientific expertise to design the most adapted solution to your goals.
Find us on: