arrow
Back to blog

KOTLIN VS JAVA: WHAT SOFTWARE ENGINEERS CHOOSE FOR ANDROID DEVELOPMENT

clock

22 min read

Google declared Kotlin as a first-class language for writing Android apps in 2017 and the preferred one in May 2019. Competition between Kotlin and Java was a stumbling block in Android development for a long time, so we must see what are the advantages of Kotlin vs Java for Android app. Despite the fact that Kotlin and Java are no longer rivals, developers and business initiatives continue to use Java for a variety of reasons, and it will require some time, expertise, and effort to complete the transition.

In this article, we will review the advantages of Kotlin, discover where Java still reigns supreme in the creation of a mobile app and digital business assets, whether Kotlin is preferable for Android development, and understand how long it takes a developer to switch from Java to Kotlin. And, of course, investigate if Google was right about Kotlin.

This post is based on a team member’s personal experience and covers fifteen points where Kotlin benefits over Java from the perspective of an Android developer, as well as five benefits of designing a business app in Kotlin.

Why may an app developer switch from Java to Kotlin?

Since its beginnings, Java has been the dominant language of Android development. The competitor language did not appear until the day Kotlin was announced. Some think Kotlin is Android’s Swift, while others argue it’s an ugly language with a bad design. Fortunately, all of Kotlin’s critics were wrong, and it is now the first official language of Android development. Once you get well acquainted with a new language, you won’t require Android convert Kotlin to Java code online.

Consider yourself an expert Android developer who has spent years creating Java code. Is there a compelling reason for you to switch from Java to Kotlin, a buzzing new language?

We all know how the tech world works:

  1. A new technology pops up.
  2. The entire community goes crazy about it, proclaiming that this newcomer IS the future.
  3. Eventually, everything will die down in a year or two at most. A fresh and flashy technology vanishes.
  4. Meanwhile, robust and dependable old techniques, languages, and approaches remain and continue to serve their mission.

What is Difference between Kotlin and Java in Android?

Kotlin turned out to move a little differently from the usual trends. There are a bunch of reasons for its success, as well as advantages for app developers in switching from Java to Kotlin and the key difference between Java and Kotlin in Android they have:

  • Kotlin code is compiled to Java bytecode (on JVM). It’s fully interoperable/compatible with Java, meaning that there is no need to change your existing code to start using Kotlin at all. App developers benefit from the ability to switch between languages at any time and convert Java to Kotlin online, at any stage of the mobile app development process, while saving time on code compilation.
  • Kotlin provides a developer with everything Java does, plus more. It brings a wider variety of modern approaches to syntax and coding (though not all those methods are newly invented). The flexibility of a programming language’s syntax allows app developers to easily build complex functionality while writing fewer code lines

The Dashdevs developer’s flashback:

When I saw Kotlin syntax for the first time, I was disappointed and tried to throw it out of my head. Though Java is verbose, I liked its syntax. In Java, to understand the code, you just need a glance, and you will identify what is what and what it is for and be ready to proceed. While the Kotlin syntax… well, it didn’t impress me. There were no semicolons, which I felt was unsafe for some reason. It lacked explicit variable types here and there. And oh, by the way, in Kotlin, you need to put a type after the variable’s name. You’re obliged to specify if it’s a variable or a value (final) by adding the explicit keyword: “fun” for methods and “constructor” for constructors… My experience in Java was just the opposite, and I didn’t understand why I might need such keywords. Moreover, it was announced that Kotlin solves nullability issues. So I tested the statement and saw that it’s not accurate. It’s still up to you as a developer, who still must handle everything yourself. No magic NPE disappearance has happened, obviously. And, uh, haven’t we used @Nullable and @NonNull annotations before?

That was how I thought back then. The reality turned out to be a little different, though.

One of Dashdevs’ Android developers had to jump into the ongoing project written half in Java and half in Kotlin. As a Java developer, he joined when the development was in progress and led the project up to its delivery and release.

That is what he remembers:

I’m not the most eager learner (which is bad). So it would be painful for me to work on this hybrid. It was how I thought it would be until I started.

How to Convert Kotlin to Java?

If you want to convert code from Kotlin project to Java online, it entails only two steps: one should compile the Kotlin code to JVM bytecode and then decompile the bytecode to Java code.

If you are struggling to learn how to work with both Java and Kotlin, the example of a developer from our team demonstrates that, in reality, the transition from Java to Kotlin is considerably easier. Android Studio (as well as Idea IDE) has a tool that can convert Java to Kotlin online and vice versa. The engineer learned what Kotlin is and how it works by converting a few lines of code back and forth. It took a Java developer less than a week to get entirely immersed in Kotlin. It was said that Kotlin is a fun language that solves several problems for Java developers and is quite simple to learn!

One of the problems with Java was the absence of a straightforward mechanism (albeit this is a controversial point) to send a function as a parameter without requiring the creation of new interfaces, runnables, or other workarounds.

Among the other annoyances were the innumerable “if (something!= null)“s (or the absence of one in the correct place), extensive verbosity, a lack of smart casts, a lack of string templates, and so on. As soon as the difficulties were resolved a long time ago, there was no need to recall all of the issues that had been causing developers problems.

So, what exactly is Kotlin for the professional Java app developer? There is more to it, but consider it an enhancement layer on top of Java.

Advantages of Kotlin vs Java for Android app development

Well, let’s delve into what pros and cons of Kotlin over Java may appear while working with it. This comparison is made in the context of Android development, so the Java version here can be considered the 8th or even the 7th.

There is not a comprehensive list below, but only a couple of prominent things to highlight.

1. Null Safety in Kotlin vs. Java

When dealing with an object in Java, a mobile app developer should be attentive. An object may hold a real value, or there may be a null as well. In Android development, the issue is usually solved by either making some assumptions here and there, by adding appropriate annotations, or maybe by using Optionals. Nevertheless, since there is no means to prevent developers from making a mistake, sooner or later they will make it.

In Kotlin, the type system identifies references that can hold null and those that can not. So if a mobile app engineer specifies that a field is non-nullable, there won’t be any nulls. The difference between Java and Kotlin is in the syntax, in adding a question mark after a type: String vs. String?. So when you have a function with a non-nullable argument, you put it the following way:

fun actionWithNonNull (argument: String) {
println(argument)
}

An Android developer (as well as a compiler) is confident that the argument is never null, so there is no need for any additional checks.

However, if you specify an argument as a nullable type, it looks like the one below:

fun actionWithNullable (argument: String?) { ... }

In Kotlin, it means that there may be a null as an argument, so a developer needs to handle nullability. The solution is similar to @Nonnull and @Nullable annotations, but the difference is that in Kotlin the code won’t compile if an app developer does not handle nullability. An engineer also won’t be able to pass a null to actionWithNonNull() function.

Such an approach forces a mobile app developer to consider nullability and determine whether it is truly required. Kotlin helps to get rid of unexpected nullability. In cases where nullability is required, Kotlin ensures complete awareness of possible nulls and provides more convenient ways of handling nullability-related issues.

Let’s say you have some repository that is nullable and you need to fetch some data using it. Below is a Java and Kotlin comparison of the same sample code.

In Java, code may look the following way:

public class Test {
    @Nullable
    private Repository repository;

    private void displayRating() {
      if (repository != null) {
         User user = repository.fetchUser();
         if (user != null) {
            if (user.rating != null) {
              Integer amountOfStars = user.rating.amountOfStars;
              displayInfo(amountOfStars.toString());
              return;
            }
         }
      }
      displayInfo("Error");
     }
    }

And below is how the same sample code may look in Kotlin:

class TestKotlin { 
   private var repository: Repository? = null 
   
   private fun doSomething() { 
 val amountOfStars: Int? = repository?.fetchUser()?.rating?.amountOfStars
   val info: String = amountOfStars?.toString() ?: "Error" 
   displayInfo(info) 
  } 
 }

As you can see, it looks a lot better in Kotlin. There is no nested if’s, no nothing. Of course, this is just a ridiculous example, but it reveals some perspective.

2. Fields and constructors in Kotlin compared to Java

Another important point to compare Kotlin and Java is the way they work with fields and constructors.

Let’s say you have a couple of fields in your class with some accessors. As simple as the next thing in Java:

class User {
    private String firstName;
    private String lastName;
 
    User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Now let’s see what the same code looks like in Kotlin:

class User(var firstName: String, var lastName: String)

That’s it—the same functionality within a single line in Kotlin.

Moreover, if you add data at the beginning of the line and make it a data class:

data class User(var firstName: String, var lastName: String)

You’ll get equals(), hashCode() and toString() for free.

Also, if you need some custom setter/getter, you can have them using Kotlin’s properties:

class GrandpaUser(grandpaName: String) {
    private var tellMeWhoToBeCounter = 0
    
    var name: String = grandpaName
        get() = "Ye olde $field"
        set(value) {
            tellMeWhoToBeCounter++
            field = value
        }
}
If you need a private setter—you can have it as well:
var name: String = grandpaName
        get() = "Ye olde $field"
        private set(value) {
            tellMeWhoToBeCounter++
            field = value
        }

However, if you need a public setter and a private getter as well, then, unfortunately, you can’t do that in Kotlin ATM. You’ll need to write plain Java-style getters and setters.

3. Named parameters and default values in Kotlin

In mobile app development, you may encounter a function that receives multiple arguments of the same type, which might be problematic:

fun createDetail(isAvailable: Boolean, isReliable: Boolean, isComposable: Boolean)

In such cases, to be certain of what you’re passing, utilize named parameters:

val detail = createDetail(
	isAvailable = false,
	isReliable = true,
	isComposable = false
)
An app developer can use the same approach to instantiate an object:
val detail = Detail(
	isAvailable = false,
	isReliable = true,
	isComposable = false
)

Thus far, a Java developer would undoubtedly like Kotlin’s default values.

If some parameters need not be passed, you do not require several overloads of the function or a constructor. In Kotlin, there is a more straightforward solution—just add a default value:

fun createUser(name: String, isSuperUser: Boolean = false,

groupName: String = "default")
...
val newUser = createUser("John Doe")

There is a default value for the isSuperUser and groupName me arguments in the supplied sample, so you don’t need to send them if we don’t want to.

However, what if you want to specify groupName while omitting isSuperUser? You can do that too:

val newUser = createUser(name = "John Doe", groupName = "lucky")

Kotlin allows you to leverage this and make a simple copy of the object with just minor modifications by utilizing the copy() function, which is accessible for data classes:

 data class User(
       val firstName: String, 
       val lastName: String, 
       val group: String, 
       val bloodType: String
)
 
  val father = User("Butterscotch", "Horseman", "regular", "AB-")
  val son = father.copy(firstName = "Bojack")	

As a result, you get a son user with the changed first name “Bojack”, and everything else is the same. Doesn’t it seem quite convenient?

4. Lambdas and functions when writing in Kotlin instead of Java

Kotlin allows you to store functions in variables and data structures, as well as pass functions as arguments to higher-order functions and return them. Assume there is a need to pass a lambda to a method. Lambdas are not defined functions in Kotlin; they are passed as an expression. In such instances, app developers in Java frequently create an interface with only one method. A developer does not need to construct anything to use Kotlin instead of Java; they simply pass the lambda:

fun callingFunction() {
    lambdaReceivingFunction { users ->
	    users.filter { it.age > 18 }.size
	}
}
 
 fun lambdaReceivingFunction(action: (List) -> Int) {
    val users: List = repository.getUsers()
    val result = action(users)
    displayInfo(result)
}

There is also an option in Kotlin to have functions outside of the classes, and even assign a function to a variable if required. Functions are objects in Kotlin, though a developer might not even pay much attention to them.

And, of course, in developing a mobile app, a software engineer can have an object containing a lambda as easily as the following:

val someLambda: (String) -> Int = { it.length }

So with all this being said here, you might be worried about memory allocation overhead. There’s a solution for that too: a developer can use inline functions. Basically, instead of creating a function object for the parameter and generating a call, the compiler will put the body of your function in place of its call.

Now let’s add an inline keyword to the mentioned above function:

inline fun lambdaReceivingFunction(action: (List) -> Int) {
...
}

And, we can call this function the same as before, YET after compilation, the caller function will logically look the following way:

fun callingFunction() {
    val users: List = repository.getUsers()
    val result = users.filter { it.age > 18 }.size
    displayInfo(result)	
}

There are some limitations to this approach, but for most cases, an app developer can use it to reduce any overhead.

(Also, have you noticed that filter() function is used in the example above? Kotlin provides tons of handy extensions for collections that a developer can use out of the box).

5. How to use Kotlin Extension Functions in Java?

Since we’ve been talking about functions in mobile app development, it would be a crime to omit extension functions.

Let’s say a developer has to validate some string in Kotlin.

fun validate(String stringToCheck): Boolean {
    return stringToCheck.length > 0
}
 
...
 someString: String = someObject.someString
 isValid: Boolean = isStringValid(someString)

That’s fine. What if, instead of our regular validate() function, we create an extension function?The result can be much nicer! Here’s an extension function on String class:

fun String.validate(): Boolean {
    return length > 0
}

And then a caller code changes to:

val isValid: Boolean = someObject.someString.validate()

It looks like validate() is a function of the String class, but it’s not, indeed. It was an app developer who wrote it, and it’s outside of the String class, obviously. Within an extension function, a developer can access public methods and fields of the class the one is extending by using this reference. Pretty neat, huh? Moreover, if a function would make more sense as a property, a developer can have an extension property too:

val String.isLongName: Boolean
    get() = length > 20

and then use it:

val isLongName: Boolean = someObject.someString.isLongName

6. Expression body functions in Kotlin

Is it possible to make a mobile app code even better-looking? If you code in Kotlin, there is definitely a way! Let’s say we have this regular function that combines first and last names:

fun combineNames(firstName: String, lastName: String): String {
	return "Mr/Mrs $firstName $lastName"
}

Since it is a single-line code, we can get rid of the return type (a compiler will automatically infer it) and omit a couple of other redundant things too, and now here’s what the result will look like:

fun combineNames(firstName: String, lastName: String) = 
"Mr/Mrs $firstName $lastName"

7. Singletons in Kotlin

This one is the shortest point. Need a simple singleton while developing a mobile application? In Kotlin, you’ve got one:

object Pony {
    fun prance() {
        ...
    }
}
And then use it like this:
Pony.prance()

The Pony is (certainly!) initialized when Gandalf arrives; it is accessed for the first time.

8. String templates in Kotlin

You might be well familiar with such a thing as a string template in multiple different languages for mobile application development. This string template may seem like a small thing, but it is nevertheless convenient and powerful.

A developer has the option to replace string concatenations with prettier and more convenient string templates.:

val message = "User ${user.firstName} ${user.lastName} (${user.age})
wants to add you to $groupName"

Nice and easy!

9. Smart casts in Kotlin

Let’s suppose there is a class CoolUser that extends class User, and a developer wants to do something if a given user is cool:

In Java, the code would look something like this:

if (user instanceof CoolUser) {
    ((CoolUser) user).doCoolThing();
}

While in Kotlin, thanks to smart casts, an app developer can do it the following way:

if (user is CoolUser) {
    user.doCoolThing()
}

Or maybe you’d prefer a safe cast operator as?

(user as? CoolUser)?.doCoolThing()

As mentioned in the example, if a software engineer has performed a check once, the code below assumes the check’s result automatically, and there is no need to cast explicitly.

10. “When” expressions in Java and Kotlin for Android App Development

So when smartcasts are combined with when, they truly shine.

Imagine there are a couple of classes extending User: CoolUser, AngryUser, IndifferentUser, and each of those has methods specific to their types. Let’s say there is a need to perform different actions based on the user type. Thus there is a bit of a different approach with “when” expressions in Java vs Kotlin for Android app development. To do this in Java, a developer can use if/else. In Kotlin, though, it can be handled a little more graciously:

when (user) {
	is CoolUser -> user.doCoolThing()
	is AngryUser -> user.doAngryThing()
	is IndifferentUser -> user.doIndifferentThing()
}

Isn’t that concise? Moreover, an app developer can do multiple checks:

when {
	(user is CoolUser) && (user.age > 18) -> // Do something...
	(user is IndifferentUser) || (user.age > 18) -> // Do something
	else -> // Do something else	
}

A software engineer can also use when as a statement and return a value:

val title = when (user) {
	is CoolUser, is Indifferent -> user.firstName
	else -> user.lastName
}

11. Differences in a protected modifier’s usage in Kotlin and Java

Have you, as an Android app developer, ever wondered why protected members of a class are package-visible in Java? Our experts certainly had. In Kotlin, as it is expected, a protected member is accessible only from within the class itself and from its subclasses. A little more secretiveness is here, in Kotlin.

12. Asynchronous work in Kotlin versus Java

If you’re developing an Android app, there’s a 100% chance you’ll have to deal with asynchronous operations. A developer might use AsyncTasks, Loaders, Executors, Threads, or something else. There’s a good chance a developer won’t be happy about using any of the above. Kotlin supports all of these approaches as well as another method for dealing with similar tasks: coroutines. Coroutines are lightweight threads that allow an app developer to convert asynchronous code into synchronous one. However, we won’t dig deeper in this article, as coroutine is a topic that deserves a couple of articles. Below is an example just to give a glimpse of what a coroutine can look like.

fun displayPurchases() {
	launch(Main) { // launch coroutine on the main thread
		val purchases = withContext(IO) { // switch to IO thread
			// Fetch data:
			val user = repository.fetchUser() // long-running operation
			repository.fetchUserPurchases(user.id) // long-running operation
		}
		
		displayPurchases(purchases) // display result on the main thread
	}
}

The code within the launch { } is executed synchronously, so a developer doesn’t need callbacks or anything else. It works just like a regular code.

We start a coroutine on the main thread. Then we fetch data on the background thread (using withContext(IO) to switch threads). After all, we proceeded with the result of repository.fetchUserPurchases(user.id) being saved to purchases. After that, we return to the main thread and display the purchases as usual.

In this case, we need a**user**’s id to fetch purchases, so we wait until a user is fetched. If, in your case, you want to execute multiple calls in parallel, it can be done with the help of async/await. Let’s say there is a need to get 2 lists and then return them combined.

suspend fun getExpenses(): List =
	withContext(Dispatchers.IO) {
        val main = async { repository.getMainExpenses() }
        val additional = async { repository.getAdditionalExpenses() }
        return@withContext main.await() + additional.await()
    }

(Yes, in Kotlin, a mobile app developer can add 2 lists just like that, using the plus operator).

As you can see from the example, everything in Kotlin is quite simple and basic. There are no callbacks or code fragments. A developer starts long-running, parallel operations, waits for them to finish, and then returns results.

13. Delegates in Kotlin

Kotlin contains a set of default property delegates as well as the ability to construct custom ones.

There’s one delegate that might be crucial for mobile app development, which we would like to mention: lazy.

Consider a field that an app developer may or may not need to access at some time. However, the object is large enough that it makes a developer wonder: do I need to create it if there’s a chance I won’t use it at all?

A developer can resolve this issue with the help of lazy delegate:

val errorHandler by lazy { ErrorHandler() }

Having errorHandler declared like in the example below, the object won’t be instantiated until it’s accessed for the first time. So here’s another of Kotlin’s useful features for mobile app development.

There’s also a possibility to have implementation by the Kotlin delegation. Refer to the sample below:

interface MyInterface
 
class SomeClass : MyInterface
 
class Other (val myInterfaceImplementation: SomeClass) : 
MyInterface by myInterfaceImplementation

It’s time to emphasize that a developer can use var (as in variable) or val (as in value, read-only variable) variables. At first, it may seem annoying to specify it explicitly, but It significantly improves code.

In contrast to Kotlin, a developer in Java might achieve the same outcome by utilizing the final keyword. Yet, Kotlin necessitates more deliberate decisions on the part of the software engineer. As a result, it is difficult to forget to make a choice. In addition, if a developer commits an error (e.g., makes it a var and then assigns value once), a lint warning will appear, indicating that a variable can be converted to val.

14. Collections in Kotlin

We’ll try to keep it brief since this is a vast topic.

So, how would you like it if you could make a list like this?

val myList = listOf("First", "Second", "Third")

Or a map:

val myMap = mutableMapOf(
    "First" to 1,
	"Second" to 2,
	"Third" to 3
)

and use it as seen below:

val first = myMap["First"]

Or maybe you would like to remove items from the map the next way:

myMap -= "Third"

How about mapping, zipping, associating, filtering, sorting, and a slew of extra functions for collections right out of the box?

You can do it all using Kotlin.

15. Other Kotlin advantages compared to Java

The list could go on since Kotlin has so many valuable features. Android-specific extensions have not even made the cut, despite the fact that there are many of them in Kotlin and they are excellent.

Consider the following case: if developers access Java’s getters/setters from Kotlin, they can access them as if they were properties. So if you have a Java class User, then in Kotlin, you can access it in one of the following ways:

user.getName()

or

user.name

And also

user.setName("NewName")

or

user.name = "NewName"

As you can see, such an approach not only enhances developers’ experience while working with Kotlin code but also when accessing Java code.

Disadvantages of Kotlin for app development

Despite the various benefits, let’s take a more precise look at both the advantages and disadvantages of Kotlin over Java in Android, as maybe find a few things that an app developer may miss while switching from one language to another.

1. No ternary operator (a ? b : c) in Kotlin

Kotlin doesn’t have it. Although a developer can make use of if/else statements to achieve similar outcomes: val user = if (oldUserIsValid) user else User(). Yet, it’s not the same.

2. No static members in Kotlin

Yeah, there’s no such thing in Kotlin. But here’s a solution: a developer can add a companion object.

Class User {
    ...
    companion object {
        fun staticFunction() {
        }
    }
}

Then call it the way a developer does with static methods:

User.staticFunction()

3. No implicit widening conversion in Kotlin

If you’re used to doing things like this in Java:

int someInt = 0;	
 long someLong = someInt;

The bad news is that it will not compile in Kotlin. A developer must explicitly convert types:

val someInt: Int = 0
 val long: Long = someInt.toLong()

Build speed and runtime allocation overhead in Kotlin vs. Java

There are some disagreements on build speed and runtime allocation overhead. If you believe it may be a roadblock for your project, you should read the following articles:

(Be aware that they did not use Gradle and did not use incremental builds for the annotation processor (Kapt) for their research, so it affected build speed).

In general, if you don’t abuse language features and use Gradle daemon with incremental builds, the number of differences compared to Java build time will be negligible.

Benefits of Kotlin mobile app development for business

Keep in mind that we’ve barely scraped the surface with a slew of “benefits” mentioned above that may seem to be sugar examples you can live without. When all of the aforementioned components are combined, the way Android developers produce and comprehend code changes significantly. So when it comes to Android apps development using Java and Kotlin, the latter always wins.

Why Choose Kotlin Over Java?

  1. With Kotlin, your business app code is less verbose. Because of the reduced codebase and more clear code, developers save time on code processing tasks such as onboarding and code reviews.
  2. The Kotlin code is written faster due to its developer-friendly structures and more elegant syntax. For example, the Hello World program code in Kotlin may be 51 characters long, while the same code consists of 116 characters in Java.
  3. Due to addressing the main pains of app development and syntactical solutions, Kotlin makes it easier to write code. A business can develop a high-quality, complicated app with fewer resources.
  4. The Kotlin code is safer and less error-prone. Businesses rarely think if they should write Android app in Java or Kotlin, as an app written in Kotlin can be more robust and save even more time and resources on the testing stage.
  5. Due to the slow pace of new Java versions’ adoption in Android, Kotlin allows functionality for mobile apps that won’t land in Java. Kotlin is a relatively new language, created by the same company that created the Android Studio’s backbone IDE. For at least four years, this language has been utilized for Android development. Google has supported Kotlin and it has been a first-class citizen in Android development for around two years. When opening the Android developer documentation, you’ll find two tabs on the example snippets: Kotlin and Java, in that order.
  6. Moreover, you can target JavaScript and use Kotlin for web development. Developers can use Kotlin DSL with Gradle. Also, there’s an experimental Kotlin Multiplatform which allows writing business logic shareable between different platforms.

After reading this article ask yourself “Should i use Kotlin or Java for Android app development?”.

There is no longer any debate that Kotlin is worth investigating—the answer is absolutely yes. Some people may be disappointed by new products that others are excited about. If you try to create apps in Kotlin, you might find that it’s not for you, but at least you’ll discover what it is. If you are bored of working just for Android, consider branching out into cross-platform app development; in this case, our article about React Native and Flutter may be handy.

We’ll see how Kotlin app development evolves in general, but in the world of Android, Kotlin has become № 1.

Share article

Table of contents