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.

References

  1. https://kotlinlang.org/
  2. 10 Kotlin Tricks by Jake Wharton - https://www.youtube.com/watch?v=0sPzDwS55wM
  3. https://medium.com/@magnus.chatt/why-you-should-totally-switch-to-kotlin-c7bbde9e10d5
Advertisement