Why I love Kotlin for Android
I have recently started working on a new Android project that I decided to use Kotlin for as per Google’s recommendation, and I am so glad I did.
It reminded me of my experience back in 2011 when I switched from C++ to Java. Back then, it was amazing for me how I don’t have to deal with pointers or segmentation faults anymore.
Switching from Java to Kotlin for Android development gave me an even better experience for so many reasons, but I am only listing a few here.
Parcelable
For those of you who ever needed to pass an object from one component to another, you must have needed to make your class implementParcelable
, and I am sure you can agree on how painful that was.
Here is how you need to do it for a class with only two simple members in Java:
This is how you would accomplish the same in Kotlin:
Yes, that’s it. Believe it or not, those two Kotlin lines do exactly what you did using all those lines in Java. The same goes if your class has more complex members, such as another class that implements Parcelable
.
Finding Views
You have a TextView
with android:id="@+id/hello"
in your activity_main
layout file. To access that view in Java, you would use the well known findViewById()
function:
Although you can still use the beloved findViewById()
method, you don’t have to. By importing synthetic properties, you can use the layout views as variables with the view ID as the variable name:
Null Safety
Kotlin makes it almost impossible to get a NullPointerException
unless you really really want to. By using null safety operators (namely ?
and !!
), Kotlin not only does make sure that you don’t get that annoying exception anymore but also makes it much easier to check for null objects.
Here is a very common example in Java. Let’s say you passed an object from one activity to another as an extra added to the activity result intent. Now, to safely parse and use that object in Java, you would use:
Now, let’s see what that translates to in Kotlin:
That oneliner gets you that integer member for which you used all those lines to safely get in java.
Setters and Getters
Kotlin replaces Java setter and getter methods with property access syntax. In the previous example, we used intent
instead of getIntent()
and extras
instead of getExtras()
. You would only use the function call in the case you need to pass an argument to the getter, e.g. getParcelable(TAG)
.
Another very common example is when you read or write a value to a view. For example, to get and set TextView
text in Java:
In Kotlin:
Singleton and Utility Classes
To define a utility class in Java, we typically use a final class with a private constructor and all static methods:
In Kotlin, we use an object
declaration:
No need to state the obvious
Finally, here are a few more examples of how Kotlin saves you the burden of writing unnecessary code:
- Class members are public by default unless otherwise noted
- The return type is not required if a method returns void
- Braces are not required for empty classes
- Semicolons are not required unless you have multiple statements in one line
That’s all I can recall for now. If you are already using Kotlin for Android, and you have something else you love about Kotlin, please share in the comments.
If you haven’t started using Kotlin for Android yet, I really believe you should get started. If you disagree, let me know why.