Kotlin is a statically typed language developed by JetBrains that targets the JVM. If you have worked with Java before, you know how verbose it could be. When Apple released Swift, developers rejoiced for a better alternative to Objective C. Kotlin could be that alternative for Android developers working with Java. Let’s take a look at some of its amazing features:
Compatible with Java
It’s completely compatible with Java meaning that Kotlin and Java can work well in the same codebase side by side. No need to rewrite your complete Java project into Kotlin. You could shift to Kotlin gradually and keep your old legacy Java as it is.
Simple Syntax
It is easier to read, the syntax is lean and very intuitive.
Null safety
Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake
var output : String
output = null
And of course, Kotlin protects you from mistakenly operating on nullable types, including those from Java
println(output.length())
And if you check a type is right, the compiler will auto-cast it for you
fun calculateTotal(obj: Any) {
if (obj is Invoice) {
obj.calculateTotal()
}
}
Of course, there are ways to avoid the NullPointerException
, just check for null
, right? Not quite! Having null in the type system is vastly superior to any sort of nullability checks or annotations available in Java.
Extensions
Kotlin, similar to C# and Gosu, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions. Kotlin supports extension functions and extension properties. Extensions do not actually modify classes they extend, they are resolved statically.
Markdown Docs
The language used to document Kotlin code (the equivalent of Java’s JavaDoc) is called KDoc. In its essence, KDoc combines JavaDoc’s syntax for block tags (extended to support Kotlin’s specific constructs) and Markdown for inline markup.
Lambdas
Android applications require a lot of callbacks, listeners due to their inherent asynchronous nature that depends on user interaction. With Java, it tends to get verbose very quickly:
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
// Do something
}
});
With Kotlin lambdas, it gets very concise
button.setOnClickListener({ /* Do something */ })
It’s even more useful when you use RxJava to chain Observable
s into a long stream reducing code to ten lines what could easily go to a hundred lines in Java.
Utility methods
Apart from the major additions, Kotlin provides several minor features that go a long way in making your life easier.
- It has hundreds of utility methods like
map
,truncateAt
etc. ==
operator does what you expect- String interpolation is possible:
println("Name $name")
Android Extensions
Every Android developer knows well the findViewById() function. It is, without a doubt, a source of potential bugs and nasty code which is hard to read and support. While there are several libraries available that provide solutions to this problem, being libraries dependent on runtime, they require annotating fields for each View.
With Android Extensions on Kotlin, you don’t need to add any extra code to start referencing views from your layout files.
// Using R.layout.activity_main from the main source set
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.setText("Hello, world!")
// Instead of findView(R.id.textView) as TextView
}
}
textView
is an automatic extension property for Activity, and it has the same type as declared in activity_main.xml
.
All you need is to enable the Android Extensions Gradle plugin in your project-local build.gradle file:
apply plugin: 'kotlin-android-extensions'