Kotlin vs Java Comparison for Your Next Android Development

Kotlin vs Java: Comparison for Your Next Android Development

Java has been an official development language of Android since the beginning. Java had gone largely uncontested as the king of native Android development up until Google I/O 2017, when the Android team announced another official development language, Kotlin.

Kotlin is a statically typed programming language that has been developed by JetBrains since 2010. Advocates of Kotlin, including Google’s Android team, believe that it makes Android development faster and more fun. Kotlin targets Android, JavaScript, Native, and the JVM and draws inspiration from Java, C#, Scala, and Groovy.

Is Java the best choice for developing my native Android app, or should I learn Kotlin?

This question has crossed the minds of many people, including seasoned mobile developers, innovative team leads, entrepreneurs with ideas that aim to make the world a better place, or simply students making their theses.

Today, we are going to help you answer that question.

Kotlin vs Java: Which is Better?

Java has been around much longer, has a larger community of developers, and a wider range of applications. We’ve been doing just fine so far, no need to reinvent the wheel, right? Java is great, but it’s quite old. Old wheels are replaced with newer and better wheels, and that’s why you don’t see many cars with wooden carriage wheels.

Many developers believe that Kotlin is the next step in the evolution of native Android development. The general consensus in this camp is that it makes writing Android apps easier and more enjoyable.

We’ll tell you why we think Kotlin and Java are both viable options, and then you can decide which one is right for you.

Why Kotlin is awesome

The folks over at Stack Overflow conducted a survey with over 100,000 developers, and Kotlin came in second as the most loved programming language (it was narrowly beaten by Rust). Let’s look at some of the reasons why developers might prefer it to Java.

  • Kotlin is 100% interoperable with Java. No need to abandon that awesome Java library you love; it can be reused or easily converted to Kotlin! You can easily call Kotlin code from Java and vice versa. This means that developers can use the existing Java frameworks and libraries in their Kotlin programs! Kotlin IDEs include Android Studio and IntelliJ IDEA, which both have a built-in Java-to-Kotlin converter.
  • Kotlin is more concise, resulting in less code and better readability!

Expect to see roughly 30-40% fewer lines of code than its equivalent Java program. How is this possible? Things like less boilerplate code and optional semicolons help.

Let’s take a look at simple Java class:

public class App {
  private String name;
  private String author;
  private String purpose;

  public App(String name) {
      this(name, "", "");
  }

  public App(String name, String author) {
      this(name, author, "");

  }

  public App(String name, String author, String purpose) {
      this.name = name;
      this.author = author;
      this.purpose = purpose;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  public String getAuthor() {
      return author;
  }

  public void setAuthor(String author) {
      this.author = author;
  }

  public String getPurpose() {
      return purpose;
  }

  public void setPurpose(String purpose) {
      this.purpose = purpose;
  }

  @Override
  public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      App app = (App) o;

      if (name != null ? !name.equals(app.name) : app.name != null) return false;
      if (author != null ? !author.equals(app.author) : app.author != null) return false;
      return purpose != null ? purpose.equals(app.purpose) :app.purpose == null;
  }

  @Override
  public int hashCode() {
      int result = name != null ? name.hashCode() : 0;
      result = 31 * result + (author != null ? author.hashCode() : 0);
      result = 31 * result + (purpose != null ? purpose.hashCode() : 0);
      return result;
  }

  @Override
  public String toString() {
      return "App{" +
              "name='" + name + '\'' +
              ", author='" + author + '\'' +
              ", purpose='" + purpose + '\'' +
              '}';
  }
}

And its Kotlin equivalent:

data class App(var name: String, var author: String = "",
                   var purpose: String = "")

In this comparison of Kotlin vs Java, Kotlin’s data class is far superior to the standard Java class.

Another example is how Kotlin handles constants.

Java:

 final String coolString

Kotlin:

val coolString

Kotlin uses var for mutable variables and val for immutable variables. In addition,   the compiler will also do automatic type inference and doesn’t require a cast operator.

  • Null safety is baked into Kotlin’s type system. Variables are non-null by default, enabling null checking during compile time which greatly reduces the occurrence of the dreaded NullPointerException during runtime. If you want a nullable reference, you’ll need to add a question mark after your data type like this: var secret: String? secret = null.
  • Coroutines are a great way to write asynchronous code in a more natural way. They are essentially light-weight threads that are more clear and concise. In Java, you would typically create background threads to manage intensive operations. This often led to increased complexity and headache frequency. Coroutines also enable a lot of other possibilities such as concurrency, actors, and much more. If you haven’t tried asynchronous programming in Java you might not be jumping for joy, but we love that the team at JetBrains decided to add coroutines to Kotlin!
  • Kotlin/Native. We can compile Kotlin code to native binaries! As of writing, Kotlin/Native is still in beta, but there is huge potential here and it is highly anticipated. We can share platform code and write native applications for iOS, MacOS, Android, Windows, Linux, and WebAssembly.

Why Java is still  a solid choice

Java isn’t going anywhere for a long time. There are over 7 billion devices running on Java and over 12 million developers. Let’s explore some of the reasons why you might stick to Java for your Android development.

  • Tried and tested. Developers have been building all sorts of systems and applications with Java for over 20 years. Kotlin’s first officially stable release was early 2016. Java has stability and has stood the test of time, will we be able to say the same for Kotlin?
  • It is much more than just Android. Java continues to be one of the top languages for server and web application development. Google Search, Facebook, Amazon, YouTube, Gmail, and Netflix are some of the popular sites that use Java in their tech stack. The broad range and flexibility of Java are particularly valuable to developers because they can shift to developing web and server apps if they decide that Android development is not for them.
  • The supply of skilled developers. There are millions of Java developers. According to Stack Overflow’s 2018 developer survey, Java is the second most popular programming language after JavaScript. You might have a harder time finding proficient Kotlin developers. If comparing the availability of Kotlin vs Java developers, Java is the clear winner.
  • Comfort. Java works for you. A task that seems difficult or confusing to others, is not a problem for you. You have grown accustomed to the quirks and particularities of Java and you don’t mind them. And the challenges that Java presents don’t faze you, they inspire you! You appreciate the power of Java, and you enjoy using it. Comfort is one of the most common reasons why developers choose one technology over another, and this mindset is particularly prevalent in senior developers of any programming language.

Kotlin vs Java performance

Here, Kotlin and Java pretty much equal. We feel that the difference in performance and compile time is not large enough to be a deciding factor, but here are the details:

  • When it comes to clean builds, Java compiles 10-15% faster than Kotlin.
  • With Gradle daemon running and incremental compilation turned on, Kotlin is as fast or slightly faster than Java.

Which should I use for my next Android project?

Now that you (hopefully) have a better understanding of the advantages that both Kotlin and Java bring, you can make a better-informed decision when choosing Kotlin vs Java for your Android development. Here are our reasons why you should choose one over the other:

  • Kotlin is Java with modern features and enhancements. Most developers like these changes, but others don’t. Decide how you and your team feels.
  • If you are a relatively new developer deciding whether to use Java or Kotlin for your first project or thesis, we highly recommend going with Kotlin; it’s much more modern.
  • If you feel comfortable with Java and don’t want change, you don’t have to.
  • If you are feeling adventurous and want to explore the new and exciting features, why not give Kotlin a chance?
  • Kotlin is very similar to Java, but there are differences that still must be learned. Any time spent to learn Kotlin is time not doing development.   
  • Java still has much more community support, online code, and references. Kotlin support is getting better, but Java still far ahead in this aspect.
  • Many developers who have made the switch from Java to Kotlin believe that it is more concise and makes developing Android apps easier and more enjoyable. However, not all developers share that opinion and their blog post comment sections have hosted very interesting discussions and debates.

If you still can’t make up your mind and want some free personalized advice for your particular situation, get in touch with us (we don’t bite, I promise). We are pretty good at mobile and web app development.

Contact us now to start building your team!