Kotlin vs Java for Android Development: Is It Time to Abandon Java?
Senior Android Developer
Senior Content Marketer & Technical Expert
This text is contributed by Karlo Karagic, Senior Android Developer at ITMAGINATION.
If you are asking this question in 2022, we think you already know the answer, and are only seeking confirmation. Nevertheless, unchallenged truths become dead dogmas: this blog post is intended to be a short reference point for all who ask this question. We will try to explain which language you should pick in which scenario.
Is It Time to Ditch Java? Short Answer?
Almost always, the answer is going to be yes. In our opinion, Java is a great learning language due to its verbosity, which gives it a clear and easy-to-understand syntax. Verbosity, often treated as an anti-feature, is a plus here. Oracle’s language has a lot of useful features that make it simple to learn it and understand. Because of this, it's a great language for fresh grads and junior developers to start their programming careers with. We’ll cover details of this aspect in the following sections.
An Even Longer Answer
In this section, you can find several real-life scenarios that could reasonably happen in your app development lifecycle, to better consider all circumstances you may meet.
Creating a New App
We think that when creating a new mobile app, this is an easy decision. You should go with Kotlin. The app will probably be used for years, and we don't think that Google will drop support for Kotlin any time soon, nor are there any reasons for the move.
When it comes to Java, it stopped being recommended by Google for years now. You may safely consider new apps being written in Java as having a ton of “technical debt” right from the start because it will have to be rewritten in Kotlin at some point.
You Have Many Fresh Grads/Juniors on Your Team
Hopefully, you have someone to guide those young guys in their day-to-day challenges, and they are not writing production code by themselves. If that is the case, we would still go with Kotlin because you will have to, most likely, rewrite everything if you start with Java.
If you are planning to build strong programming fundamentals in the juniors on your team, a small internal project could be written in Java. Kotlin has a lot of syntax sugar which hides some complex implementations and concepts. Make it harder for your newbies to write an app at first, to train them better.
Apps in Kotlin are usually 40% shorter than Java apps, which is great if you have developers who can utilize this speed boost. For learning purposes, however, this means it's a bit harder to read and understand the code. Additionally, Kotlin tends to make some stuff too easy, so fresh developers could start out with a few missing “chapters” in their development. One great example is concurrency in Java. Kotlin hides away the intricacies all too well.
To sum up this paragraph: if you are working only on production code, write it in Kotlin with close cooperation with more senior developers. If you have some small projects or assignments, or are building an internal app, ask them to write an MVP (Minimum Viable Product) in Java first. After that, they may switch to Kotlin.
Winner? Usually Kotlin, sometimes Java.
You Care About App Build Speed
If, for whatever reason, you prioritize build speed over all else (maybe due to really short release cycles) you may be tempted to write it in Java, as it is considered to have a shorter build time. There are some caveats here:
It is true. Java is faster for cold builds (building the app from scratch) by up to 15% depending on your device configuration. It is almost on par with Kotlin when it comes to incremental builds, however. As you are making incremental builds 95% of the time, cold build performance penalty doesn't matter that much. If you are interested in this topic, specifically, go here.
If you still think the speed of Java is worth it, let’s look at it from a different angle. Let's say you get a 15% performance boost when building the app (in all cases) which saves you 15% of your computing resources. This is not a small difference. What about your development time? Saving time for computers will be a trade-off: your people will lose time.
To reiterate, Kotlin apps are, on average, 40% shorter which equals shorter development time and fewer places for bugs to hide. Let's say this gives a 15% performance boost. Saving this little on computing costs means nothing, when you may save much, much more money if you cut your development time.
Computers are getting faster, and faster, so the gap in compilation time will diminish over time. Building the same 100k line app (100,000 lines of code) on an Intel Mac from 2020 and an M1 Mac from 2021 went down from 8 minutes to 2 minutes.
All Your Code Is in Java
Good news! Kotlin and Java are completely interoperable. This means that you can write one file in Kotlin and one file in Java, and they can work together pretty seamlessly. This means there is absolutely no reason for you not to try writing new features in Kotlin (or rewriting old ones). Crucially, you may use Java in Kotlin, however not the other way around.
As the time goes by, when you add more Kotlin code, you will probably want to move your entire codebase to Kotlin. There is an automatic tool which does that for you, though be careful it is not 100% safe, so exercise caution. Nevertheless, it can speed up the process a lot.
Let’s take a look at an example. We will calculate the Nth Fibonacci number, and use Java code from Kotlin. Here goes:
All Your Teammates Know Java and Don’t Know Kotlin
Even if this is your case, it is worth to move to Kotlin. You will probably take a short-term kick to performance (maybe a bug here and there) but mid-to-long-term you will see improvements to speed and stability. Also, don’t forget, that Kotlin is one of the most loved programming languages in the world, and it is a joy to work with.
As we mentioned already, Java and Kotlin are interoperable and as such, transition for developers can also be gradual. Your team can write just the new features on Kotlin and pick up the new language on the way. It just takes getting used to the few new features and some syntactic sugar. Luckily, Kotlin documentation is superb, and it should help a lot.
You Want to Be Future-Proof
We already mentioned this, but to recap, Google did not drop support for Java (yet?), and they are recommending Kotlin for a few years now. Not many people/companies are investing in the Java ecosystem for Android, as the focus has shifted to Kotlin. Keep in mind that Java is currently at version 17 while the Android ecosystem is stuck with Java 11. This will not change any time soon, either. You can find more details here.
New libraries for Android development are released for Kotlin. A good example of this is Jetpack Compose - a modern toolkit for building native UIs which is, most likely, the future for Android development (the UI layer at least). This toolkit does NOT work with Java and as far as the Compose team said, they don’t have plans to change that at this moment, if at all. This could be one of the biggest reasons not to use Java for projects that will go on for some years in the future, as you are locking yourself out of this great tool.
As things stand currently there is no reasonable way to write an iOS app in Java and nothing is on the horizon.
Kotlin on the other hand does have an ace up its sleeve. Kotlin Multiplatform Mobile or KMM for short. In short, it allows you to share code between Android and iOS apps written in Kotlin. The best part it is it allows you to make as much of the functionality shared as you want, as it also allows you to write native code as well. The solution might not be the most practical at the moment, though we expect the situation to improve.
You Want to Hire a Lot of New Android Developers
Considering that Kotlin is the suggested language to use for Android development, we think this is the one to use here as well. There are a few things to think about here:
Any newer developers will probably focus on Kotlin for Android, as it is the suggested language for native development.
Any older experts who don’t know Kotlin and haven’t even heard of it obviously don’t keep in touch with modern Android development and should probably be avoided (we are generalizing here, use common sense).
It's a fun experience to move from Java to Kotlin, while it's not so enjoyable the other way around. In other words, you can lure Java specialists to the Kotlin world, but it will be very hard to do the same the other way around.
Winner? Java. Just kidding. Kotlin!
The conclusion is obvious. You should really focus on Kotlin in 2022. It's a really nice programming language with tons of features. It is loved by developers, it improves the speed of app development and lowers the number of bugs.
Kotlin also has more prospects with constant improvements to libraries, fast progress of the Kotlin language and support for Jetpack Compose (the next big thing in Android). On the other hand, Java for Android is stuck in 2018 with Java 11.
If you are very engrossed in the Java world, Kotlin made it ridiculously easy to switch. You may learn as you go, develop only new features in Kotlin, or even parts of features. The documentation is excellent and there are many Kotlin developers on, e.g., Stack Overflow, willing to help out.
If you're looking for expert Kotlin developers to help you out with your development, make sureto reach out!
360° IT Check is a weekly publication where we bring you the latest and greatest in the world of tech. We cover topics like emerging technologies & frameworks, news about innovative startups, and other topics which affect the world of tech directly or indirectly.