Java has become legacy. It can’t evolve in to a modern language while keeping its backward compatibility. But it has given us a wonderful JVM ecosystem and lead to creation of many good languages Groovy, Scala, Clojure, Kotlin.
Kotlin was born in 2011, But it gained popularity last year after google announced it as official language for Android. Kotlin has brought powerful features from many other JVM languages. Let take a quick glance at benefits of Kotlin for a Java developer.
Every Java programmer hates Null Pointer Exceptions thrown at Runtime. Kotlin provides first class support to avoid null references at compile time. All objects are non-nullable by default and you have to use ?
operator to define nullable types. Compiler will force you to use safe call operator ?.
to access nullable objects. You can also use elvis operator ?:
to assign default values.
Kotlin automatically infers types, so you don’t need to declare it explicitly. You can simply define variables using val
for final variables and var
for non-final variables. Note type can be inferred only if both declaration and assignment is done in single statement.
In Kotlin you can use String templates for easier formatting of Strings. $
is used to reference a variable and you can use ${}
for complex expressions
Kotlin provides data
classes for objects used to simply hold values. It automatically generates equals
, hashCode
, toString
, copy
, getters
and setters
( for properties defined as var
) methods for data classes. You can also do object deconstruction of data class to extract properties to variables.
Kotlin supports named method parameters so you don’t need to create builders in most cases. Also, Kotlin supports default method parameters so you don’t need to create redundant overloaded methods to pass default values.
Kotlin provides concise way to initialize collections inline using listOf
, mapOf
, setOf
methods. Maps also support intuitive syntax key to value
for initialization. It also provides deconstruction of Map key, values for easy iteration.
Kotlin supports object
declaration to create Singletons in single line.
In Kotlin, constructs like try
and when
are expressions that returns value. For e.g You can assign result of try
to a variable instead of creating a local variable. Similarly for when
can be used as expression. when
is equivalent to switch
in Java, but it is much more powerful.
Kotlin provides is
operator (equivalent to instanceOf
in Java) to check if object is specific type . Using is
operator will automatically do casting for you. This will prevent Class Cast exception when you cast to wrong type.
Kotlin provides with
construct to easily call sequence of methods on same object without having to repeat the variable name. We generally use builder pattern and method chaining to that in Java. Kotlin makes it easy to do similar thing even for non-builder classes.
Kotlin also provides apply
extension function to achieve the same thing.
To favor composition over inheritance, we often use delegation or decorator pattern, but we had to duplicate every method of delegated class in wrapper class. Kotlin provides first class support to simplify delegation using by
operator. It will automatically implement necessary methods to call methods of delegated class. Of course, you can override specific methods when need to.
Kotlin supports functions outside of classes, so you don’t need to create a class just for static utility functions.
Java supports lambda by automatically substituting it with anonymous inner classes, but you can’t modify non-final variable inside lambdas. But in Kotlin you can also modify non-final variables within lambda.
Kotlin provides very simple way to lazily initialize a property using lazy
keyword.
Kotlin interoperates with Java seamlessly, so you can easily integrate with legacy Java code. You can continue to leverage third party Java libraries and frameworks. Unlike Scala, Kotlin doesn’t have it’s own collection library, but extends JDK collections. So you don’t need to write glue code to convert between Java and Kotlin collection types.
Kotlin uses method name convention to overload many operators for readability. For e.g method plus
is used to overload +
operator, minus
for -
operator, times
for *
operator, div
for /
operator and so on. It supports overloading many more operators like %, += , +- , ++,--
Kotlin provides concise way to define ranges using ..
operator. It also provides until
keyword for excluding boundaries and step
operator for skipping items. It has in
operator to check for something in range. We can also overload ..
operator and in
operator by implementing rangeTo
and contains
methods.
Kotlin support easy way to add extension functions to existing classes. This is a very power feature that helps us to easily expand core language API.
Kotlin is awesome but it’s not yet perfect. It will take some time to evolve in to a great language. JetBrains and Google are actively behind Kotlin so you can sure be that it will only get better.
If you are an Android developer, you should start using Kotlin immediately.
If you are a Java developer, you might have to consider other factors like team members, company adoption etc. But even if you can’t use it immediately, you should definitely learn this modern piece of beauty.
Finally, As per thoughtworks technology radar , companies can kotlin try in projects that can handle some risk.
Update on Oct 10, 2018:
Kotlin has moved up from Trial to Adopt category in thoughtworks technology radar, so companies can adopt using kotlin in projects when appropriate.
Should I learn Kotlin or stick to Java?_In GoogleIO 2017 Google announced Kotlin as an official language for Android development. Some famous developer…_medium.com
Kotlin in Action Book By Dmitry Jemerov (Author), Svetlana Isakova (Author)
If you enjoyed this post, Please clap so that it will reach more audience. I am planning to write deep dive articles on some of the Kotlin’s features. If you like to read more about Kotlin, follow me so that you will notified on new stories.