Published Jul 24, 2018

Share this article

Ever since Google made Kotlin the official language of Android, I had tried to look at it. As usual I tried it out as part of RandomContact app and just used a few simple features and found it to be consice and powerful. So when I wanted to rewrie MessageCounter app to make use of Android Architecture Components, I decided to jump all in and rewrite the entire app in Kotlin. Below are few snippets that I came across and liked a lot.

  1. Data Classes
    Creating a data or POJO class is so easy using the data keyword. It automatically generates a constructor, all setters and getters as well as equals and hashcode methods. This reduces a lot of boiler plate code especially if there are a lot of members. We can also create secondary constructors which have can assign default values for certain fields.
  1. String Concatenation
    Rather than join some strings using + operator, kotlin provides String interpolation as below.
  1. Creating lists or maps
    After creating a List type object, I was looking for an add method which was not found. Thats when I understoord that Lists are immutable by default. You need to create a list of type MutableList to be able to add data to it.


  2. 10 Kotlin Tricks by Jake Wharton -