I have been working on Java for at least 7 years now. It is still my first language of preference. It is a very old and stable language with a large ecosystem around it. The stability of ecosystem and mature libraries allows us to write production ready application with confidence. However, it lacks many features that the modern programming languages like Ruby, C#, Python and others offer.

Even though I prefer verbosity in my code, I have always been annoyed by the boilerplate code that Java projects require. Since past year and half I have been working with C# at work and I have absolutely fallen in love with the syntax it offers (specially LINQ).

I recently got introduced to Kotlin while watching a YouTube Video from Google I/O '17 and got immediately sold on it. Following are some of the main reasons why I will be adapting Kotlin.

Kotlin is more concise

Lets face it, Java code is overly verbose. We need to write at least 3 lines of code to write a simple Hello World Program. Following is example of Person class written both in Java and Kotlin.

// Java Person
public final class Person {
  private String name;
  
  public Person(String name) {
    this.name = name
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  @Override
  public String toString() { /* Implement this */ }
  
  @Override
  public boolean equals(Object o) { /* Implement this */ }
  
  @Override
  public int hashCode() { /* Implement this */ }
}

// Kotlin Person
data class Person(var name: String)

Larger the number of lines of boilerplate code, greater the chance of introducing bugs and maintenance overhead. Compilers are good at finding common errors than humans. We should let it do the job while we focus on creative tasks and better architecture of the software.

NULL safety

NullPointerException is every Java Programmers nightmare. Wouldn't it be nice if we could get rid of it all together? Good news is, Kotlin helps us exactly achieve this. In Java, the following code will throw NullPointerException.

Person person = null;
person.getName(); // throws NullPointer Exception

In Kotlin, we can avoid such errors as the following code will throw compilation error.

val person : Person;
person = null; // compilation error

All objects are not nullable by default. We can override this by changing its type to be nullable by using ? operator.

val person : Person? = null; // this is legal

We should never make an object nullable unless we have a very good reason to do so. We can prevent null pointer exception on nullable objects by performing good old null checks.

val person : Person? = null;

if (null != person) {
  val name = person.getName();
}

Java interoperability

According to Kotlin website, it was designed with Interoperability with Java in mind. Existing Java code can be called from Kotlin and Kotlin code can be used in Java code. Java methods following getter and setter convention are represented as attributes in Kotlin. Pretty much all Java methods can be used in Kotlin without issues.

// Person.java
class Person {
  private String name;

  public String getName() {
    return name;
  }
}

// Person.kt
fun doSomething() {
  // using Person from Java code
  val person = Person();
  
  // we can call Java getter as attributes
  val name = person.name; 
}

Easy to adapt

The learning curve of getting started with Kotlin is minimal. If you can write Java well, you would not have much problems with Kotlin. It integrates with all the existing Java ecosystem which makes it even easier to adapt. Intellij IDEA makes it really easy to type Kotlin code. IDEA comes equipped with many tools that will make adapting Kotlin a breeze. You can even select some Java code and convert it to Kotlin with just few mouse clicks.

Full stack development

This is another main reason why I will be adapting Kotlin. Kotlin can compile down to Java byte-code, JavaScript, Android and even to native code. The idea here is you can use 1 programming language to write your server side, front end, Android, iPhone or Desktop applications. This will allow us to seamlessly use and share common code between different stacks.

This is not a brand new idea and NodeJS has been successfully selling this idea of same code base in server and back-end. It uses JavaScript or TypeScript to write code that runs on server or browser. You can use tools like NativeScript to then build iPhone or Android applications from the same code base. This is great but I never got sold to this idea. I have been working with JavaScript for over 10 years, I think its a great language. But its lack of type safety will create lots of headache in maintaining large code base. It is easier to introduce bugs in the JavaScript code.

Using Kotlin you can achieve the benefits of one language for full stack development. Kotlin strongly typed and its modern feature allows us to focus more on writing great applications while minimizing bugs in the code.

Officially supported by Google

There was uncertainty in the early days of Kotlin. Some developers were uncertain if Kotlin will last very long in the Java ecosystem. In Google IO '17, Google officially announced its support for Kotlin in Android platform. This can only get better from here on and we can expect Kotlin to blend more into the ecosystem. This is a huge win for Kotlin and its future looks very bright.

Kotlin bleands seamlessly with Jetbranins set of tools and IDEs and makes it very easy to adapt. Jetbrains have heavily invested on Kotlin and they will continue to do so. Kotlin is here to stay!!

Conclusion

Many languages have come and gone in past 10 years. Scala and Groovy were decent but never received support from larger companies. Java's change rate and feature parity with other modern programming language like C# has been very slow. Kotlin compiles down to Java byte codes and runs on the JVM. In this regards it is not much different from Java at the core. Kotlin is a better version of Java and can be adapted with confidence. If you are planning to start learning new programming language Kotlin should be on top of your list.