Switching from Java to Kotlin: 5 exciting features for Android Developers

After Google I/O 17, it became clear that Kotlin will no longer be only a trend, but will soon start becoming a standard for Android development. Obviously, soon is now and it’s happening. In addition, Android Studio 3.0 fully supports Kotlin and there’s no longer need to bother about compatibility. It’s hard not to be excited by its several features for developers switching from Java. Let’s have a look on my own favourites 👍

1. Named arguments

Every Java developer’s nightmare: enormous number of parameters (ex. in Cursors or SQLite queries creation in Android), their names and order in method definition. Without documentation or IDE help you simply can’t write it properly in Java:

Fortunately, Android Studio team added useful feature that highlights names of params passed to such a function. However, Kotlin just make it easier without help of IDE:

No matter how we order our arguments list. Code looks much more cleaner and now we can, in a blind of an eye, see what we’ve just passed to the method. It’s so convenient!

2. Data classes

That’s probably one of the most useful and “spectacular” options for Java coders that are new to Kotlin. Data classes makes life of every developer easier, as it represents our beloved POJO classes in just one single line of code.

Java in its finest:

Kotlin ❤ :

What’s done here under the hood? The compiler automatically derives the following members from all properties declared in the primary constructor:

  • standard methods of Any class (equivalent of Object in Java): equals(), hashCode(), copy()
  • toString() of the form Person(name=Michał, age=22)
  • componentN() functions (more in docs)
  • we don’t really have getters and setters in Kotlin, we use object properties

It helps to get rid of boilerplate code we usually don’t want to maintain — in Kotlin, compiler does it for us. From now, instead of repeating the same code again and again, we can focus on actual functionality of our application.

3. Null safety and safe calls

I call it my billion-dollar mistake…At that time, I was designing the first comprehensive type system for references in an object-oriented language. My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
— Tony Hoare, inventor of ALGOL W.

Well, one of Kotlin’s main aims was to get rid of null reference exceptions. It still provides nullable types, but only for being fully cooperative with Java code. Default Kotlin types are non-nullable and if you try to asign them as null, it won’t let you compile.

var bundle : Bundle = null //won't compile
var bundle : Bundle? = null //nullable type, it compiles

Typical situation in Java:

…and exactly the same in Kotlin:

Null checks simply let us write much less code and force us, reckless developers, not only to make our app work better, but also to make it look better. Isn’t it amazing?

4. Extension Properties

Extension properties help me every time I need to remove repeatable casts. In traditional way, we could write this as below:

var myBigDecimal = 26 as BigDecimal
var otherBigDecimal : BigDecimal = myBigDecimal

So, let’s use our extension property instead:

val Int.bd : BigDecimal                           
get() = BigDecimal(this)

After we’ve extended Int class, we can now easily cast our Ints to BigDecimals. It works exactly the same:

var myBigDecimal = 26.bd
var otherBigDecimal : BigDecimal = myBigDecimal

We’ve done everything without having to inherit from the class or use any type of design pattern such as Decorator. Kotlin provides the ability to extend a class with new functionality in quick and simple way.

5. Smart casts

Kotlin is quite inteligent when it comes to casts :) Let’s look at this short, simple code snippet:

fun demo(x: Any) {
if (x is String) {
print(x.length) // x is automatically cast to String
}
}

Keyword is is equivalent of instanceof in Java, but it has its own powerful feature. Compiler checks instance of the object in if statement, and then sees it as String in if body. The same happens in when statements:

when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}

This is only a brief presentation of smart casts possibilites. There are also “unsafe” casts, “Safe” (nullable) casts… Check out the Kotlin docs for more.

Summary

As a newbie to Kotlin, I’m so excited in new language. Additionally, JetBrains tools make it easier to start coding — automatic converter of Java code to Kotlin, full interoperativity of the language with Java and well prepared documentation. Future of Android being more dev-friendly is bright!

Android Developer / Telecommunications student @ WUT / mobile dev enthusiast / Android passionate / cat lover