DEV Community

Krzysztof Jackowski for Netguru

Posted on • Edited on • Originally published at netguru.com

Kotlin vs. Java: Which One You Should Choose for Your Next Android App

If you're a mobile app developer, Java is probably your go-to language for building Android apps. But there are new languages popping up all over the place that might challenge Java's dominance in the Android world.

One of them is Kotlin, a relatively new programming language, which has been already announced by Google as a “first-class” language supported on Android.

Although it is constantly being developed and improved, Kotlin is already considered a mature ecosystem and its popularity is growing rapidly, especially on the mobile development scene.

Here's a detailed comparison of Kotlin and Java to help you decide which language will work best for your next mobile development project.

What is Kotlin?

Kotlin was designed by programmers from JetBrains (the guys behind integrated development environments) to add some modern features to Java mobile development.

It has definitely gained momentum after being announced as an official programming language for Android at Google I/O in 2018. Google has also internally switched to using Kotlin instead of Java on Android.

Kotlin is an open source, statically typed language based on the Java Virtual Machine (JVM), but you can also compile it to JavaScript or native to build code that can run on iOS. All it takes is installing the Kotlin Plugin and letting it configure your project.

Pros of Kotlin

  • Kotlin has a lot of traction in Android development, but it’s also used in backend projects such as Spring 5;
  • Kotlin is not only able to compile to almost every platform including Android, JVM, JavaScript, and native - using the Kotlin Multiplatform framework you can extract one common codebase that will target all of them at the same time;
  • Thanks to the language’s scripting capabilities you can use Kotlin directly in your Gradle build scripts;
  • Switching from Java to Kotlin is easy – just install the Kotlin plugin, add it to the Gradle build files, and click 'Convert';
  • It supports modern programming concepts like extension functions, higher-order functions, delegates, and more out-of-the-box to help devs build clean APIs;
  • Kotlin provides a built-in null safety support which is a lifesaver, especially on Android, which is full of old Java-style APIs
  • It's way more concise and expressive than Java, which means less room for error;
  • You can write new modules in Kotlin and be sure they will work alongside the existing Java code; Kotlin is compatible with all Java libraries and frameworks, the JVM, and can integrate with the Gradle or Maven build systems;
  • Devs can benefit from a rapidly growing collection of open source projects on GitHub, many books, learning resources, and online courses;
  • Adopting Kotlin doesn't cost anything (except for learning and training).

Cons of Kotlin

  • Rather steep learning curve when switching entire teams to Kotlin due to the language's concise syntax (both a blessing and a challenge);
  • Slower compilation speed than Java (though Kotlin was shown to beat Java in some cases);
  • Relatively smaller developer community compared to Java, which means limited learning resources and difficulty in finding answers to questions; one look at Stack Overflow will give you a rough idea – there are only around 26.000 questions tagged with Kotlin against 1.5 million questions about Java (June 2019);
  • There are more and more Kotlin developers available, however finding an experienced mentor for your team might require a bit more effort;
  • Kotlin, being a highly declarative language, sometimes tends to generate great amounts of boilerplate in corresponding JVM bytecode;

Kotlin use cases

Here are a few amazing apps written in Kotlin:

What is Java

Java is the favorite of many developers when it comes to Android app development – mainly because Android itself used to be written in Java.Developed by Sun Microsystems (now the property of Oracle), Java is an object-oriented programming language that boasts the title of the second most active language on GitHub. And no wonder – it’s been around for over 20 years, and its popularity only seems to grow.

Pros of Java

  • Easy to learn and understand;
  • Flexible – you can run it in a browser window or a virtual machine. This comes in handy when you reuse code and update software;
  • Android relies on Java – the Android SDK contains many standard Java libraries;
  • Java has a large open-source ecosystem, partly as a result of Google's adoption of the Java Virtual Machine (JVM) for Android;
  • Accelerated assembly within Gradle – we're all thankful for it when assembling large projects;
  • Java apps are more compact and are easier to be optimized when better performance is required. Also, in comparison to Kotlin, Java apps tend to be lighter (even in full analogue) as Java is a more imperative language;

Cons of Java

  • Java has limitations that cause problems with Android API design;
  • As a verbose language, Java requires writing more code, which carries a higher risk of errors and bugs;
  • It’s slower in comparison to many other languages and requires a lot of memory.

Java use cases

Here are some examples of prominent open source apps written in Java:

  • Android-oss - the official kickstarter.com Android app, a social platform for funding creative projects;
  • NewPipe - a lightweight video streaming app project for Android;
  • Wordpress Engineering - the official WordPress App for Android. (Although new features are developed in Kotlin, the core codebase is still made up of Java code.)  

Why use Kotlin for Android

You may wonder - what is the point of making the switch from Java to Kotlin?

And the main argument might be the fact that Kotlin is designed to address Java’s issues. It means that Kotlin provides many safety mechanisms available out-of-the-box while being more concise and expressive than Java at the same time.

Perhaps that's why Kotlin is among the top five languages most beloved by developers, which accounts for % of developers who are developing with the technology or language and have expressed interest in continuing (Stack Overflow 2019, 100.000 respondents).

the most popular programming languages

Credit: "Developer Survey Results 2019" by Stack Overflow

Conclusion

While Kotlin offers many advantages that Java doesn't, it still has some shortcomings. When letting your team experiment with Kotlin, remember that transitioning to a new language isn't always that exciting for developers who have already found the tools and strategies that work for them.

You'll need to give your team at least a month to get familiar with Kotlin, risking that some team members might not adapt in time for the launch of your Kotlin project. You also need to bear in mind that Java isn’t going anywhere and will remain essential for Android app development.

But if you're a beginner looking for a new challenge, it makes sense to try Kotlin, especially if you'd like to work on innovative projects for startups. If you're running a team of mobile developers, however, it's best to experiment with Kotlin one step at a time to check whether the new language brings you tangible benefits.

Top comments (4)

Collapse
 
jillesvangurp profile image
Jilles van Gurp

Google recommends using Kotlin on Android, always and primarily. Non buts, no ifs. That's the official word on what to use. They of course continue to support Java because they have a huge amount of legacy apps that they can't break and APIs that they need to continue to support for years to come but it's very clear that at this point it's a second class citizen. All of the new stuff they are doing is Kotlin centric and I'd say pretty soon there's going to be a lot of Kotlin only stuff that flat out won't work on Java or at the very least is going to be tedious to deal with (e.g. anything related to co-routines).

So, you should have a very good reason to stick with Java for new projects on Android. Most of the advantages of Java you list apply equally to Kotlin. A huge con that you miss is that Google stopped updating their language support and never got around to support e.g. all Java 8 features and will probably never bother to do this due to the ongoing conflict with Oracle. Java 7 was released nearly 9 years ago and at this point is quite hopelessly outdated.

So they are not actually advantages relative to Kotlin; it's just keeping up with it.

For your list of Kotlin use cases should probably include the vast majority of the top n applications in the app store where n can get quite large before that stops being true. You'd be hard pressed to find a top rated android app that does not use Kotlin (or in some cases native code). It's very competitive at the top. At this point you'd be a late adopter, not an early adopter if you still have to switch to Kotlin on Android.

Collapse
 
krisj profile image
Krzysztof Jackowski

Jilles, thanks a lot for your comment. You point up very important things that I didn't think of. The infamous fight between Google and Oracle was a huge pain for Android devs.
Kotlin is most definitely a first choice language for Android development at the moment when you are starting a new project. Although when you have a legacy app with a big codebase it might be tricky to start using a new language. Despite the interoperability, introducing Kotlin to the existing project may break some tools supporting development. For example, tools that count code coverage, linters, etc may require additional work to adjust everything properly.

Collapse
 
jillesvangurp profile image
Jilles van Gurp

Legacy projects that still matter should make an effort to reduce technical debt. Addressing tool issues is a part of that. Kotlin has pretty awesome tooling so addressing issues related to that should not be that hard.

Collapse
 
nmrathod00 profile image
Nayan Rathod
But if you're a beginner looking for a new challenge, it makes sense to try Kotlin

I totally agreed. If you are a newbie Android developer, I'm also suggesting to get start with Kotlin rather than Java. Kotlin has major advantages than Java. you can also check the major differences between Java and Kotlin from here: Kotlin vs Java