So Google just bestowed their blessing unto our new null-safe, statically-typed friend. Is it too early to get on board? No, it is not. Is it worth the trouble? There is no trouble.
What is it?
Just in case you’ve been living under a rock, Kotlin is a programming language developed by JetBrains. It is designed to be null-safe, concise and interoperable with Java. Learning Kotlin is really easy, especially if you already know Java. Here are three (of the many) ways in which Kotlin makes our life easier.
Null-safety
NullPointerException
is a word that gives every Java developer an instant headache. Kotlin has null-safety built into the language.
Here’s how it works — you can specify whether a variable can be null when you declare it. If it is not declared optional, it can never be set to null.
To make something nullable, you just use a question mark:
The Elvis operator ?:
is super useful when making safe calls:
Conciseness
According to JetBrains, a Kotlin program can be written in 40% fewer lines of code compared to equivalent code in Java. Data classes are the best example to illustrate this.
In Java, a typical class to hold your data model consists of the following:
- A constructor
- Getter and setter functions
hashCode()
,equals()
andtoString()
functions
The code looks something like this:
The equivalent code in Kotlin is:
Interoperability
Kotlin is 100% interoperable with Java. It is built by JetBrains so it is a first-class citizen in IDEA and Android Studio. Nothing happens to your existing code base. If you want, you can start writing in Kotlin today without changing a single character in your existing Java code. All your Java libraries work as intended. Kotlin code performs the same as the equivalent Java code.
Conclusion
Kotlin has been successfully adopted by major companies like Netflix, Pinterest, Uber, Trello, etc. and all their tech blogs point towards increased productivity and developer happiness. With Google’s recent announcement, whether Kotlin will outshine Java in the long run is a no-brainer. If you want to start writing Kotlin and make your life easier, there’s no better time to do that than now.