As a Java developer, it can be difficult to switch as Kotlin is not compatible with Java so migrating the entire code-base is impossible. In this article, I have listed five facts that should help you decide whether to start using Kotlin or stick to the good old Java. Let’s get started.
1. No raw type support
The first and foremost thing that might deter you from learning Kotlin is that it does not support raw types. That means if you pass an object of type T to a function expecting an E, then compile will fail. This might force you to write repetitive boilerplate codes for null safety such as !!X (Double exclamation mark) and also dealing with Class Cast Exceptions. This can be resolved by using safe calls operator (?) instead of the double exclamation mark operator.
I am aware that this might sound trivial to Java developers who are used to null checks in their code. However, I had come across experienced Java Developers who were unaware of how they should handle things in Kotlin when an object was null or if it had a nullable type associated with it.
For example, consider the below snippet in Java where we are attempting to load data from DB, which is stored in list. As you can see, though, there’s no explicit null check included, but it’s still safe as anything extra added after .get(0) will return false if the list is empty. However, this is not the case in Kotlin.
2. Limited Java Interoperability
This might come as a huge disappointment for Java developers. A new programming language that doesn’t allow you to access Java library methods? Well, that’s the bitter truth about Kotlin.
Kotlin is designed in such a way that it can work seamlessly with Java code, and you can call Kotlin code from your existing Java classes and vice versa. However, when it comes to calling Kotlin code from your existing java class or vice versa, then there’s no simple way of doing so without adding external dependencies, which we will cover in detail soon. You can take the assistance of the DBA administrators.
3. Less IDE Support
As I mentioned earlier, all major IDEs such as IntelliJ support Kotlin (Yes, a big shout out to Android Studio), but even then, you might find it challenging to migrate your existing projects as Kotlin doesn’t offer support to convert your Java class files into Kotlin files.
For example, consider the below snippet in Java where I have used annotations to decorate my methods or properties with @Deprecated. But if you were expecting these features in Kotlin, you are out of luck because this language does not support annotations.
At first, if you are learning Kotlin, then this will seem like an alien concept, but the good news is that there’s a workaround to convert Java code into Kotlin code by using an experimental feature called Kapt (Kotlinc-Android). Although it works pretty well, I have faced some issues using it.
4. Limited Null Checks
As mentioned earlier, Kotlin doesn’t support raw types, so if you pass an object of type E to a function expecting D. Then the compiler will simply ignore this and compile successfully. Still, once you run your code on JVM, there’ll be a Class Cast Exception thrown at runtime.
This might lead you into writing repetitive boilerplate code for null safety, such as!!x (Double exclamation mark) and also dealing with Class Cast Exceptions which is definitely not a good practice.
5. Less Boilerplate Code
As we know, Java is verbose so does Kotlin, but when comparing both languages, one thing that my friends appreciated was Kotlin’s syntactic sugar that allows us to write less code and achieve the same thing, which is very important for android development as we all know how verbose layout XMLs are.
Now, after going through these points, I hope you have a general idea about what this language is and whether it’s worth spending your time on it or not. So, that being said, the answer to that question is yes! Kotlin is a great programming language but then again, why would you consider switching over from Java when the support for android development is not very strong?
Kotlin is the future of android development, but it still needs to improve in some areas so that developers can adapt it easily.
YouTube: Kotlin Course – Tutorial for Beginners
Photo credit: The feature image has been done by Nichapa.
Did this article help you? If not, let us know what we missed.