Migrating from Java to Kotlin
Migrating from Java to Kotlin can be a bit challenging at first. However, we believe that at this point, if you’re working on an app, you should seriously consider making this step. Kotlin was designed with mobile in mind, so, once you’re all set up and got to know Kotlin a bit, it will be so much easier to develop your app further.
After we discussed this topic at one of our Cobalt Matters sessions this summer – along with a slice of yummy watermelon – we’ve put together this post for those of you who find yourselves in this situation. We hope it will help you gain a better understanding of what exactly means to migrate from Java to Kotlin. Also, we have a few reasons why we recommend to do it as soon as possible. Check them out and enjoy the ride!
Why Migrate from Java to Kotlin
1. Interoperability with Java
Kotlin compiles down to byte code, which is executed on the virtual machine. This makes it possible to have both languages within the same project, so we can start migrating step by step without worrying that we will break things if we don’t migrate all codebase at once.
4. High order functions
This will come as a pretty weird concept for classic Java developers, but it can get rid of a lot verbosity and duplication from Java. We can use high order function instead of anonymous classes from Java. One basic example is replacing listeners/callback interfaces with high order function. How will this look like?
We know how hard is to do any async work in Java. For the networking layer, we have 3rd party libraries like Retrofit, Volley, RxJava and others who can make coding a bit more fun and easier. But what about heavy computation that needs to happen locally on the device and we need to do it async? Well, AsyncTask 👎🥵 is the answer for java. For Kotlin we have coroutines. How easy this can be?
That easy. No more context and possible memory leaks. 🥳
How to Convert the Current Code to Kotlin
So we have seen a small sample of examples that show why should we migrate to Kotlin. But how can we do it? In Android Studio we can find this button if we right-click on any java class.
Is that all we have to do? Sadly, while the converter works pretty good, we still have to check for:
- !! – Usually when we convert a Java file to Kotlin, the converter will assume that we want nullable variables to throw NPEs (NullPointerException) if we don’t want this, and we never want this, we need to make slight adjustments in the code.
- Try to write as much as possible Kotlin idiomatic code. The converter will not convert automatically to Kotlin idiomatic code, so we will need to make changes, otherwise, we will have Java idiomatic code with Kotlin syntax.
- Take it slowly.
Kotlin is fun and makes Android app development better and faster. In contrast to Java, Kotlin was designed with mobile in mind. Since 2011 – when the Kotlin programming language was released – it quickly became popular in the Android community. Today, the Android team from Google has first-class support for Kotlin and Android app development is rapidly becoming Kotlin-first.
We recommend that you get familiarised with Kotlin as soon as possible – if you haven’t started already. App development is much smoother and intuitive when using a programming language specially designed for this purpose.
Curious to learn more?
You’re in the right place. Here at Cobalt Sign, we’re building apps for iOS and Android since 2011 and we love every part of it. We always stay up to date with the latest technologies and we try to share as much knowledge as we can with you.
So, if you are an app developer and want to learn more, make sure to reach out to us and let us know what topics you’d love to read about. Periodically, we will choose from the topics you propose and we will write about them.