Kotlin vs Java: For Android Apps and Beyond in 2021

by 
 Updated on 
September 6, 2020
 / 10 minutes

Before we get into the details, It is worth noting that Kotlin is designed to improve developer productivity and overcome the shortcomings of existing languages like Java and C++. So, by design, Kotlin has advantages over Java in many areas. 

Developers migrating from Java to Kotlin claim they need to write 30% less code to achieve the same objective in Kotlin as compared to Java.

While 30% productivity gain is huge, Java too has made many improvements with Java 8 onwards, the productivity gap should reduce if you rewrite java code with latest Java features. 

Nonetheless, I love Kotlin too and there is a lot more than just the productivity gain. 

Kotlin vs Java

So, the big question is whether you should switch from Java to Kotlin? and for the newcomers, whether you should start off with modern Kotlin or rely on age-old Java? 

At 2019 I/O, Google announced Kotlin as the language of choice for Android development and declared that any further progress in the Android ecosystem would be Kotlin first.

While Android apps development is not the only thing you do with Kotlin and Java, this announcement from Google ignited this Kotlin vs Java debate. 

Also, it is important to know that Google has already built 60+ apps using Kotlin including Google Play, Maps, and Home. Apart from Google, many enterprises including Uber, Evernote, Pinterest, and Atlassian have started using Kotlin in their apps. Some of them have completely upgraded the code base to Kotlin.

More than 50% of the recent apps released on the Play store are built with Kotlin, Java’s share has reduced drastically in this space.  

Kotlin’s claim to fame seems to be the concise code, lambda expressions, Null pointer exception safety, interoperability with java, and a lot more. 

Is Kotlin Replacing Java in 2021?

Well, the short answer to this is quite evident, Kotlin is fast replacing java for Android apps development. Having said that, Java is a very old language and commands a huge ecosystem. 

Java is used in many other areas apart from Android, including data science, web development, server-side programming, networking, embedded systems, and much more. So, Java is not going anywhere anytime soon. 

In fact, popular indexes and surveys still rank Java among the top 3-4 languages in terms of overall developer base and popularity, though the trend seems downwards.. 

Java also has a huge codebase and set of libraries for Android and since Kotlin is interoperable with Java, so, the Java ecosystem for Android too is here to stay for a while.  

Kotlin or Java: Which one is for you?

Let us start off with a brief introduction of the two languages before getting into an objective and parametric comparison. We will start off with Kotlin – 

What is Kotlin

Kotlin was an initiative of JetBrains for their internal development but they open-sourced Kotlin shortly thereafter. The first stable version of Kotlin came into existence back in 2016, and within a matter of months, Google announced its support for Kotlin.

Kotlin is a statically typed general-purpose programming language and runs on Java Virtual Machine (JVM). Koltin offers both object-oriented and functional programming paradigms.

Apart from Android/JVM based development, you can also compile Kotlin to javascript and use it for front end development.

Kotlin also supports server-side development by utilizing existing frameworks and libraries of java, like spring and Vaadin. You can set up a Node server too to ditch the JVM altogether.

The team behind Kotlin is also working on additional features to make it suitable for Native mobile development, data science, machine learning, and AI. 

What is Java

Java is an old programming language and has ruled the world of programming for more than a decade. Sun Microsystems developed Java back in 1990, now owned and maintained by Oracle. Writing code once to run on all platforms was the philosophy that made java so very popular. 

Apart from Android apps development, Java is majorly used for web development, enterprise software development, networking, embedded systems, data science and much more, least to say, it still tops the job charts. 

Java is also a statically typed, class-based, object-oriented programming language and brings in an imperative style of coding. Java 8 introduced many features to support the functional programming paradigm as well as features to write concise code. 

So, while Java is facing tough competition from modern languages, it is still in the game with continuous improvements. 

Java runs on Java Virtual Machine (JVM) and JVM makes Java programming platform agnostic. It is interesting to note that, as of today, it is not only Java that runs on JVM, but hundreds of other languages run on JVM.

Some of the most notable modern languages utilizing JVM include Scala, Kotlin Itself, Groovy, Clojure, Ceylon, XTend, Fantom, and X10. The list is very long, we will not go there for now!

You might like: Online Java Compiler

Why Kotlin for Android and Beyond?

Let us look at some of the key features Kotlin brings on the table that Java doesn’t have and vice versa – 

Coroutines

Non-blocking asynchronous code execution and concurrency take care of long-running and CPU intensive tasks and provide a seamless user experience. Kotlin adapted coroutines to achieve this, which is missing in Java.

In java, developers typically utilize background threads to achieve this but Coroutines in Kotlin make it much easier.

Coroutines pass the execution to the libraries while the rest of the code continues, this enables you to write concurrent and asynchronous code as required, with ease. 

Null safety

Null pointer exception, why is it so huge. Ask any Android developer working on a large application in Java and you will get to know. Null references go unhandled and end up in NullPointerException in Java, and hence frustration for developers. 

Kotlin on the other hand offers Null safety by default. Data types in Kotlin can not hold null values by default and code fails at compile time itself making NullPointerException impossible in Kotlin. Well, you can make an effort to get one though. 

Example –

var cantNullMe: String = "abc" // Kotlin default
cantNullMe = null // compilation error

Smart Casts

Typecasting is irritating and developers hate that. Kotlin offers Smart Casts that let you skip the casting. All you need to do is check an object to be of a specific type using the “is” operator and move forward. No need to explicitly cast to that object type. 

Example –

fun autoCast(x: dumb) {
    if (dumb is String) {
        print(dumb.length) // dumb auto casts to String
     }
}

Data Classes

Kotlin makes it easier to create classes that are meant to hold just data, you need to append prefix “data” to the class declaration and the basic functions for all data elements would be available out of the box, unlike java where you need boilerplate code.

Example –

data class Shape(val name: String, val sides: Int)

Developer Productivity

It is not an opinion of a few developers but a well-known fact that Kotlin requires lesser code to develop the same functionality, given its concise syntax including one-liner functions.

Kotlin is also well-packed with features to support functional programming including higher-order functions, this makes code readable and highly reusable. 

Also, since Kotlin is fully interoperable with Java, all the existing libraries and frameworks of Java can be used as-is in Kotlin without the worry to write basics from scratch. 

Extension Functions

Many modern languages like C# offer extension functions, and so does Kotlin. Using extension functions, you do not need to inherit the class to add more functions to it. 

This is specifically useful when you want to add more functions or capability to the third party provided classes/packages. You can simply define new functions using extension functions syntax and those will act as if belonging to the original class. 

Compile Time

Compile time of Kotlin has been reported by developers to be higher than Java, which is kind of a set back for projects with a large codebase. 

Though I do believe that compile-time will improve as more and more Kotlin libraries and frameworks pop up, for now, Kotlin uses many Java libraries, so the code base is mixed and results in higher build times. 

As per official notes, Google and JetBrains together are committed to reduce the compile time of Kotlin.

Lambda Expressions

Kotlin offers support for Lambda expressions. This gives a lot more flexibility to developers, developers can treat Lambda functions as arguments to other methods, use returned values as is, and for that matter, treat the function as any other object or data value.

This is more driven from the functional programming paradigm and helps keep code concise, readable, maintainable, and well, scalable too. 

Inline Functions

WIth higher order functions, you can pass functions as arguments and get return value as a function. Given that every function here is treated as an object, this becomes overhead for memory allocation and virtual calls. 

The solution to avoid overheads is inlining the functions, which avoids object creation but instead adds the code of the referred function inline the calling function. It can easily be done by using the keyword inline. 

Checked Exceptions

Java has checked exceptions while Kotlin doesn’t have it. This is something where the developer community is in favor of java, my take is, developers are too used to working with checked exceptions and finding it difficult to adapt to Kotlin for the time being. 

I personally do not like too many try-catch blocks, there has to be a better way of catching exceptions. 

I am still experimenting with Kotlin to figure out how the dropping of the checked exceptions is playing out, so, we wait and see before concluding this in favor of Kotlin or Java. .

Ease of Delegation by Design

Delegation as its name suggests is about passing operations or processes from one object to another.

Kotlin has first-class support for delegation to achieve this as compared to inheritance in Java, this kills the need for any boilerplate code and helps build a scalable codebase. 

Static Keyword

Static members are used in Java heavily but Kotlin does not provide support for statics. While Kotlin does not support static, it has alternatives to achieve the same.

Static keyword in front of a field or class ensures that a separate instance of that type can not be created.

For example, you can create a function outside of the class, at the package level directly, and this function will have only one instance always. 

An even better way to do this in Kotlin is to use companion objects.

In Java
class Foo { public static int a() { return 1; } }
In Kotlin
class Foo { companion object { fun a() : Int = 1 } }

Final Thoughts: Is Kotlin Better?

From a language capability perspective, Kotlin has clear advantages. And, specifically since it is interoperable with Java and can utilize everything that Java has. So, it has the upper hand by design. 

In the Android development space, Kotlin has already taken the front seat replacing java.

More apps are built using Kotlin than java as of writing. So, there doesn’t even seem to be a Kotlin vs Java debate anymore, go for Kotlin.

Java, on the other hand, has a huge ecosystem and skilled developers available. It may not be wise to touch your existing codebase and start porting it to Kotlin with full force. You might want to continue with Java skills on the existing code, and start small on your migration endeavor.

0 Comments
WRITE A COMMENT

Leave a Reply