Will it be KOTLIN or JAVA for ANDROID development in 2020?

Despite the widely used Java as a programming language for android application development, Kotlin takes over approximately 1/4 of popularity ever since it was announced as an official language since Google I/O in 2018.

Introduction

For Android development, Kotlin has been around for 2 years. Kotlin, developed by JetBrains is statically typed language and widely used because it is robust and less verbose than Java.

While Java, an object-oriented programming language developed by Sun Microsystems has been around for 2 decades. Also, Java is the favourite of many developers when it comes to Android app development mainly because Android was written in Java.

Pros of Kotlin over Java

1) Null Safety

Kotlin type system has inbuilt null safety. This feature keeps away from NullPointerException which we can consider a common mistake for android development. Because all types are non-nullable in Kotlin by default. So, this feature reduces the chances of null pointer exception in development.

While Java doesn’t have such an inbuilt feature to avoid NullPointerException. Here kotlin wins over Java because of the inbuilt Null Safety feature.

e.g.

Student student =null // Java
val student: Student? = null // Kotlin

Java

Student student = new Student(1,”XYZ”)
student =null // will compile and throw null pointer exception

Where as in Kotlin

val student = Student(1,”XYZ”)
student = null

student.name	  //won’t compile
student?.name 	  // will compile. not throw null pointer exception here 
student!!.name	  // will compile. If the name found null it throws NPE 

2) Interoperability

Due to the Interoperability feature, we can use both Java and Kotlin languages in a single project. You can call Kotlin functions in Java class as well as Java methods in Kotlin class. This gives you the advantage of code reusability even language is different.

You can use existing Java project's classes and functions in the new Kotlin module without rewrite in Kotlin from scratch or without any change in it. The same way you can call Kotlin classes and functions in Java without any change in classes and functions due to Interoperability feature.

Thanks to Kotlin compiler which enables two languages to work together in the same project due to this feature. Hence, Kotlin wins over Java here.

3) Code Conciseness and readability

Comparing a Java class with an equivalent Kotlin class shows the shortness of Kotlin code because lots of features are introduced by Kotlin.
e.g. Data Class, Extension etc. reduces boilerplate code.

Let's compare code conciseness with the help of the Data Class feature.

Java

Student.Java

public class Card {
    private int rollNumber;
    private String name;
    // getter methods
    // setter methods
    // toString
    // equals
    // hasCode
    // copy 
}
                  

Kotlin

data class Student(val rollNumber: Int, val name: String)

In the above code, you can see that with Kotlin you have to write much lesser code and code is cleaner than in Java just by putting one simple keyword called data before the class name. Kotlin takes care of all the getters, setters, equals, toString methods and other functions for you. Writing more code means you are more prone to errors which are, obviously, something that we don’t want.

4) Supports modern programming concepts

Kotlin supports modern programming concepts like extension, new introduced operator, annotation, functions, higher-order functions, delegates, and more out-of-the-box to help developer to build clean and more readable code.

5) No Raw Type

Kotlin doesn’t support raw type which reduces the risk of CastClassException whereas Java Supports raw type which can increase the possibility of CastClassException code written in Java language.

6) No Checked Exceptions

Kotlin removes checked exceptions fully, which minimizes verbosity and improves type-safety. Whereas checked exceptions in Java is somewhat controversial feature that forces programmers to acknowledge the fact that an exception may be thrown, either by catching it or by explicitly allowing it to propagate up the call stack.

There are lots of features which will be used in modern android development, improving application performance and code conciseness, and readability.

  1. Coroutines
  2. Higher-Order Functions and Lambdas
  3. Type inference, expressions
  4. Inline Functions
  5. Native Support for Delegation
  6. Now you can write gradle scripts in Kotlin instead of groovy which is introduced in recently released gradle version 5.0.

Cons of Kotlin over Java

  • Kotlin highly short syntax, while a great advantage, does require some learning upfront. Whereas Java is easy to learn and understand.
  • Kotlin does not work well for native as well as cross-platform compared to Java.
  • Kotlin apps are not lighter and not more compact compared to Java apps.
  • Slower compilation speed compared to Java in general cases.
  • Lack of resources and community because Kotlin community is still young and the learning resources are limited. So, you will find little difficult to find an answer to problems.
  • Kotlin, being a highly declarative language, sometimes tends to generate great amounts of boilerplate in corresponding JVM bytecode.