Kotlin vs. Java: Which Programming Language to Choose for Your Android App
Kotlin vs. Java: which is better? This struggle started in 2011 with the advent of Kotlin, while at the same time, without a doubt, the “experienced” Java was completely beating any new competitors off. With the support of Google and JetBrains, Kotlin was developed to iron out any of Java’s wrinkles. Starting with the Google I/O 2017 Developer Conference, Kotlin began to kick things into high gear.
Can a newcomer completely replace good old Java? And, is it hard to find good Kotlin developers? In this article, we will review the Kotlin vs. Java question and name the benefits and drawbacks of each language to better understand who is winning the struggle now.
Let's get started and discover these software development technologies.
Kotlin vs. Java: Intro to Official Android Programming Languages
Android and Java, like fish and chips, have often been bonded together. Android SDK is even written in Java. However, with the invention of Kotlin, the situation has started to change. So what is Kotlin language and can it beat all the benefits of Java? Let’s dive into the history and check out the differences between Kotlin vs. Java to understand whether adopting Kotlin is worthwhile.
What is Java and What are its Use Cases
Java is an object-oriented, class-based programming language. It was designed by a “Green Team” led by James Gosling at Sun Microsystems (now owned by Oracle Corporation) and was originally planned to serve the digital cable television industry. However, the language appeared to be too sophisticated for this purpose and found its place instead in the world of Internet programming.
Java was released in May 1995 and today there is hardly a developer who hasn't heard of it. No wonder, as it is the second most popular language on GitHub with around 70K repositories.
As an object-oriented programing language, basic Java syntax is similar to the C and C++ languages. The whole code belongs to classes in which all values are objects. This allows for the creation of modular programs and reusable code.
Apart from using Java for Android applications, developers are also able to create desktop apps and app back ends. Unsurprisingly, as it has a tremendous community and almost 25 years of usage, Java has proven to be trusted and secure.
What is the Kotlin Language and What are its Use Cases
Kotlin is a brand new Android programming language that was first introduced by JetBrains in 2011. It got its name from the Kotlin Island in Russia and in 2017 became the officially supported language for Android development. Kotlin can also be used for developing server-side and web client-side apps.
Since the 2017 Google I/O conference, the use of Kotlin language has started to rise exponentially. This should come as no surprise, as it was made to be completely JVM (Java Virtual Machine) compatible and compiles down to Java bytecode. This means that both Java and Kotlin code can co-exist within one project.
During the Kotlin vs. Java comparison, the first thing you will notice is that Kotlin is both an object-oriented (OOP) and a functional language. It has a great support for higher-order functions, function types, and lambdas, so it can easily be used in both object-oriented and functional programming styles.
In 2018, a lot of huge companies switched to Kotlin as it is 100% interoperable with Java. Among such companies are Google, Uber, Trello, Pinterest, Kickstarter, and many more. So, why has Kotlin become a choice of so many giants, and could it really put Java in the shade?
Kotlin and Java: Do They Fit Together?
The Kotlin language was created to resolve current bottlenecks that Java has, to be more concise, and for safer and easier use. But what are the perks of adopting a new language if you need to completely rewrite everything from scratch?
Kotlin’s creators anticipated that it would be 100% interoperable with Java. Moreover, to make the process of adoption smoother and easier, JetBrain developed a Java to Kotlin converter and integrated it into IntelliJ. This works pretty well, but still may need a few human tweaks such as adjusting nulls (related to null safety) or inline variable initialization.
There is no requirement for converting applications written in Java to Kotlin, since Kotlin code can easily be called from Java and Java code from Kotlin. The majority of existing Java frameworks and libraries are also compatible with Kotlin. All of this makes the process of adoption easy and smooth.
Kotlin is supported by all major Java IDEs including IntelliJ IDEA, Eclipse, and NetBeans. Kotlin plugin is bundled with Android Studio starting from version 3.0. The language also integrates with some of the the main build tools such as Gradle, Maven, Ant, and Kobalt. So, do Kotlin and Java fit together within one project? The answer is yes, they absolutely do.
Java Android Development
Java has been at the top for more than two decades and it is still a very popular programming language. We won’t be telling how to make Android apps with Java, but will name Java advantages that make it one of the most used languages in the world.
Java vs. Kotlin: Advantages of Java
If we compare Java to Kotlin, we can see that both languages have positive characteristics worth considering when choosing between them. Those of Java are listed below:
Easy to learn
One of the biggest advantages of the Java coding language is that it is easy to learn; therefore, it is easy to write, compile, and debug using Java. The language has a digestible and understandable syntax and can thus be a good starting point when learning programming.
In addition, the number of courses that teach Java is immense and it is possible to find many resources and examples when needed. This simplifies the process of learning Java greatly.
Another Java advantage is that it has a very strong community with almost one million repositories on GitHub. Hence, there are thousands of different libraries and frameworks which can be utilized and, as a result, speed up the development process.
One more Java pro is that it is platform independent. Java compiler converts the source code to bytecode, which serves as a so-called “intermediate” language. Bytecode can be executed on any platform using Java Virtual Machine. You can even write Java code on Android using a special app called Java N-IDE.
Java vs. Kotlin: Downsides of Java
It may seem that Java is a perfect language, but just like any other programming language, Java has its shortcomings.
The verbosity of Java has become a basis for many jokes. Indeed, Java requires that far more code be written than Kotlin, and so there is a higher risk of bugs or errors.
In terms of Java vs. Kotlin syntax, we can see that that of Kotlin is clearer and more concise.
NullPoinerExceptions has become a real headache for Java developers using non-nullable variables. This null-unsafety can be frustrating sometimes since null represents absence of value. As a result, developers need to write additional lines of code to find a way around this issue.
Taking all of this into account, it may seem that making Android apps with Java is a dead-end job. But is this really so? Let’s find it out.
Recommendation: Why Use Java in 2019?
It looks like Kotlin is the perfect language to adopt, so why is anyone still making an Android app with Java? Here are a few reasons:
- You have Java gurus on your team that don't want to switch to Kotlin.
- Your team tried Kotlin and was unsatisfied with it.
- You have a successful project that was developed in Java (however, in this case, it is not a problem to develop new features in Kotlin). But, there is a small twist, in that Java code still needs to be supported by Java, if you don’t want to transition the whole project to Kotlin.
What benefits of Kotlin for Java developers are there? What are its weak points as compared to Java? Let’s check it out.
Kotlin: New Android Language
Kotlin language provides new capabilities for Android development. It compiles to JVM bytecode and can easily be adopted without dropping off or having to rewrite an entire project.
Kotlin vs. Java: Advantages of Kotlin
Despite the fact that Kotlin is a fresh language, it has already shown a lot of advantages as compared to Java. What are its benefits in the Kotlin vs. Java competition? Let’s check out why it can be worthwhile to pair Android with Kotlin.
Kotlin is proven to be more concise. The estimates show an approximately 40% cut in the number of lines of code. The code base shrinks, leaving less space for bugs and errors and, as a result, the quality increases.
One of the biggest Kotlin advantages is that it is completely interoperable with Java. JetBrains’ Java to Kotlin converter, integrated into IntelliJ, provides a smooth transition to Java code. Moreover, Kotlin supports almost all existing Java libraries and compiles into Java compatible bytecode.
Functional programming support
Kotlin supports not only object-oriented programming but also functional programming. Kotlin has proper function types built in, as opposed to Java's Single Abstract Method (SAM)-conversions, so it can help solve many problems developers face more easily.
Kotlin has a built-in fail-fast system that helps to reduce bugs and avoid errors in the code whenever possible. There are three standard library functions in Kotlin that work for this purpose: check, require, and assert.
Kotlin is a young language, but it already has quite the supportive community. There is a Kotlin Slack channel, which every developer can join to get support and help from other Kotlin devs.
Kotlin vs. Java: Weak Points of Kotlin
No language is completely perfect, and the Kotlin language is no exception. Along with a huge amount of advantages, it also has some weak points.
The code compilation process is slower, as opposed to Java. Statistics show that for clean builds, Java compiles 17% faster than Kotlin. However, for consecutive incremental builds with no files or one isolated/core file changed, the compilation time is almost identical or Kotlin can even be slightly faster.
Lack of resources
Though it was developed back in 2011, only in the last year has Kotlin started to grow its popularity. Hence, it is not yet very easy to find highly skillful Kotlin developers to teach and lead your team.
New Kotlin Features in Android Development
Let’s make a Kotlin review and find out what new features it has got in its pocket that make Android programming even easier and more sufficient.
- Null-safety through nullable and non-nullable types, safe calls and safe casts
Kotlin provides a special way to prevent null checking with special non-nullable types.
if (someOjbect != null) ...
These types don't allow setting
null in a variable. If you try, your project won't be compiled. This Kotlin feature guarantees that you won't receive NullPointerException when you use a non-nullable variable.
Another case is nullable types, which allow you to set null. However, to prevent NPE, Kotlin has special operator
?. For example.
someObject?.someMethod(). This code will be executed only if someObject is not null.
- Extension functions
This feature allows one to add own methods to existing classes. It is very useful when one needs to have extended functionality of existing classes. For example, by default, ImageView class in Android doesn't have methods to load images from the Internet, but it can simply be extended to provide this functionality. Let's say we create an extension function
loadImage(url: String). After that, you can just call
loadImage(url) on any ImageView instance and it will work.
- Higher-order functions
This feature allows developers to create methods that may receive other methods as arguments or return methods. For example, let's say we have a list of items and when a user clicks on the item, we need to redirect him to a new screen with detailed info about this item. Let’s analyze the Kotlin vs. Java way of implementing this feature.
In Java, we need to create an interface with a method, implement this interface on some object that will listen to user's tap events, and call an interface's method each time the user taps on an item.
Kotlin has a simpler implementation. We just write a function that contains all the logic that must be invoked when the user taps. So, instead of invoking interface's method (that calls a method with logic), we directly call an existing function.
- Data classes
There are a lot of use cases wherein we just need to keep data in classes (in MVP architecture it is M - Model). Kotlin vs. Java comparison will show you that Kotlin provides a simple way to create these classes. When we create a data class, Kotlin automatically generates useful functions for read/write values, for copying data, etc.
This feature allows objects to be immutable, which means that once created, an object can’t be changed in the future. If you need to change an object, you should create a mutable copy of the immutable object, change it, and continue working with it. Immutable objects are thread safe; because they can’t be changed, there is no need to sync them and there are no concurrency problems.
- Type aliases (added on Kotlin 1.1)
This feature allows for shorter names of existing types. For example, instead of using
MutableMap<K, MutableList<File>> in Java, here are Kotlin code examples you can use instead.
typealias FileTable<K> =
FileTable<SomeType> as a new type, that in fact will be expanded in
Kotlin vs. Java: Which is Better?
For many years, Java was one of the first-choices and recommended languages for developing Android apps. However, with the advent of Kotlin, it has slowly but constantly given up this place. Kotlin came from industry needs, and its syntax and structure were inspired by Java and such well-known languages as Scala and C#. It was developed to be easy to dive into and to master.
Below you can find a Kotlin vs. Java table summarizing what we mentioned in the article.
Java to Kotlin converter
OOP and FP support
|Open source (OpenJDK implementation only)
|Compilation time||Slower as compared to Jav||Fast|
|Code Length/ Readability||Concise||Overly Verbose|
|Development speed||Fast||Rather Fast|
|Community support||Growing community,
|Gigantic community on GitHub,
Reddit, and StackOverflow
|Security||More secure (thanks to null-safety)||Secure|
Kotlin is being aggressively promoted by Google and JetBrains, so it is unsurprising that more and more visible companies have publicly declared their usage of Kotlin. Every day brings new libraries and code examples.
The Kotlin vs. Java debate proved that Kotlin is more secure (hello null-safety:) so there is a smaller chance of crashes. Kotlin is more flexible, provides more possibilities that help to reduce the number of lines written and, as a result, fewer bugs and errors occur.
As of autumn of 2018, MLSDev, custom software development company has also joined the team and is now actively using Kotlin in developing Android projects. We have already written several applications from scratch using Kotlin and wrote new modules in Kotlin within existing and ongoing projects such as GoPuff.
Based on our experience and the Kotlin vs. Java analysis, we believe that Kotlin works perfectly for startups and already established companies, and it helps to stay efficient and reduce the number of boilerplate code. So let’s build your Android app with Kotlin.
Have any additional questions or want to build an Android app?
Contact us and we’ll be more than happy to assist you.