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.
var s: String = "I Love Kotlin" | |
s = null // compilation error, s can't be null |
To make something nullable, you just use a question mark:
var nullableString: String? = "abc" | |
nullableString = null // now this is not an error |
The Elvis operator ?:
is super useful when making safe calls:
if(nullableString != null) { | |
return nullableString.length(); | |
} else { | |
return -1; | |
} |
return nullableString?.length ?: -1 |
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:
import org.apache.commons.lang.builder.EqualsBuilder; | |
import org.apache.commons.lang.builder.HashCodeBuilder; | |
import org.apache.commons.lang.builder.ToStringBuilder; | |
public class User { | |
private String name; | |
private int age; | |
public User(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this); | |
} | |
@Override | |
public int hashCode() { | |
return new HashCodeBuilder() | |
.append(name).append(age).toHashCode(); | |
} | |
@Override | |
public boolean equals(Object other) { | |
if (other == this) { | |
return true; | |
} | |
if ((other instanceof Example) == false) { | |
return false; | |
} | |
Example rhs = ((Example) other); | |
return new EqualsBuilder().append(name, rhs.name) | |
.append(age, rhs.age).isEquals(); | |
} | |
} |
The equivalent code in Kotlin is:
data class User(val name: String, val age: Int) |
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.