Android - Java vs. Kotlin : Why you should use Kotlin ?
If you have ever developed an Android application ,there is a 99% chance that you have used the Java programming language ,at least if you don't use hybrid technologies such as Cordova or Ionic .
For native Android development ,Java is the most dominating language for many reasons such as being the first official language for Android platform .But it's not the only language that you can use for building native Android apps .
Theoretically you can develop Android apps in any JVM or Java Virtual Machine compatible language i.e a language which compiles to JVM bytecode such as Scala ,Groovy ,Closure and Kotlin etc.
Google has recently announced Kotlin as one of the official languages for Android so developers are starting to consider it as a viable option or alternative to Java . In this post we'll look at why you should use Kotlin instead of Java for Android development then we'll see the pros and cons of switching from Java to Kotlin .
Watch this video : Kotlin is here for Android. Is Java dead Now ?
Why use Kotlin instead of Java for Android
Java is one of the most used programming languages in the world ,it's particularly widely used by enterprises for building large scale applications .It's also the official language for Android but for many reasons it's time for another language to take its place .
The first and biggest reason is that Java isn't as modern as other existing languages ,despite the fact that Java 8 has introduced many modern features such as Lambda functions but for many reasons (legal issues) Android only supports a subset of Java 8 features so if you want to continue developing with Java for Android ,you are pretty much obliged to use Java 7 which is considered by many developer as obsolete.
Other reasons of why you should start using Kotlin instead of Java 7 for Android development are :
Java syntax is so verbose which is not the case for many modern languages based on JVM ,among them Kotlin .
Java 7 has no support for functional programming features which are only added starting from Java 8 .
There are many know issues with Java such as the Null Pointer exception .
What is Kotlin ?
Kotlin is a modern JVM based programming language just like Java ,Groovy ,Closure and Scala .It's developed by JetBrains which has also developed IntelliJ IDEA IDE for Java development and the IDE which Android Studio was built based on it.
Here is a list of Kotlin pros vs Java :
Java is a lot less verbose than Java 7 ,which means you can do more with less code .
Kotlin is safer than Java since you can control null exceptions in compile time .
Kotlin is interchangeable and completely interoperable with Java ,which means you can still use Java libraries in Kotlin and you can even mix Java and Kotlin source code files in one project.More than that you can start learning and playing with Kotlin from an existing Java project without having to create a Kotlin project from scratch .
Kotlin is functional and Java 7 is not ,it has many functional features such as lambdas ,even if it's not a purely functional language .
First class Android studio support ,by just installing the Kotlin plugin on your Android Studio you'll get an excellent support for Kotlin code.
So for these reasons ,Kotlin is considered to be one of the most excellent alternatives to Java for Android but since we are not in a perfect world ,there are also some cons or drawbacks to use Kotlin instead of Java.
You may think that the verbosity of Java is not a good thing just like any other developer but in fact this verbosity makes it easy to understand or better read a previously written code and since Kotlin is much less verbose ,this will also makes Kotlin code less readable in some situations such as unfamiliar code portions .
If you can care less about the first drawback ,you won't do the same with this second one which is related to available documentation and help , Kotlin has less tutorials on the web and much less help that Java so in many cases if you are stuck with a problem ,you are very much on your own and you can't blame anyone for this ,since Kotlin is a much recent language and a lot of developers are still learning it themselves.
In this post ,we have seen what's Kotlin and why you should use it as an alternative to Java 7 for developing native Android apps .We also looked at some pros and cons of switching from Java to Kotlin .