Let me just say this. I absolutely love Kotlin as a programming language. If I had to name a favourite, it would be that.
For 2 years, I have been using Java for Android Development. When I heard about Kotlin at the Google I/O ’17, I was so eager to try it out. I can’t emphasise just how worth it it was to take a little time to learn what seems to be an enhanced, cleaner, quicker-to-code, more error-proof version of Java.
Thankfully, the learning curve is low for any experienced Java developer. Most should be able to learn and begin programming in Kotlin in just a few hours.
Now, I try to use Kotlin as much as possible in Android Development. Even if there are still gaps in my knowledge of the language. So much to the point that it feels odd when I have to resort back to ye olde Java code.
By no means am I a Kotlin expert, but my little knowledge in it has already shown me considerable results. This may only become more prominent as I become better at the language. Let me tell you why.
1. Shorter, Cleaner Code
Let me show you a few comparisons:
1.5. Lambda Expressions
You may have noticed the above setOnTouchListener was shortened greatly, and this is through the use of a lambda expressions (the little arrow thing ->). It recognises the input parameters of the function being called, defines them on the left side of the lambda, and on the right are the operations the function will perform.
Though there are many tools and expressions in Kotlin which help you write shorter and cleaner code, this one just deserved a special mention.
2. Null Safety
Kotlin forces you to apply extra care to code that can possibly throw a Null Pointer Exception through warnings and errors such as the variable might not have been assigned a non-null value or the referenced view id is not present in all versions of a layout.
While this makes you pay a little more attention to the code you are presently writing, you WILL run into so much less errors in your development. This will ultimately increase your efficiency as a programmer.
3. No more findViewById
FindViewById, how I hate it. Not only does it make my code look horrible with entire sections of this flooding my whole computer screen, but it also makes me run into tons of errors. While it’s not 100% true that Kotlin does away with these, for the most part, it does.
Kotlin allows you to import all the views of a layout to your class so you can reference your views directly by their id. This results in much shorter, cleaner, and null-free code.
There are still some instances where you will have to resort to using findViewById (at least, in my experience), but that usually happens only in adapters where you can’t import views directly into the class. In these cases, you will often only be referencing a couple of views, so not much is lost. You also get the null-safety checks of Kotlin which is always a bonus.
Really, the only time I still use Java is when modifying other people’s classes and libraries that were already written in Java. Sometimes I even take the time to convert those to Kotlin. You have to try it out for yourself. See for yourself what everyone is going on about, and enjoy the great benefits of Kotlin.