Kotlin has taken 33% of google play store apps worldwide on last month. When devs started developing in Kotlin, It was like they were experiencing a new world of freedom to develop and to express things more beautifully and more efficiently. Kotlin comes with some cool ideas and concepts that we haven’t used in Java and also some of the existing boilerplates in Java were removed in Kotlin. This post will help you to go through some of the cool and useful concepts of Kotlin.
Let’s get into code…
In Java we will be using a bunch of setter function to the object of builder pattern. This will look like kind of weird thing when we want to set more than 10 properties. In Kotlin we can simply use the with/ apply concepts. Here is some of the methods used for initiating the class with setters in Kotlin.
with() will allow you modify function parameter instances.Basically, like in the example.
Adding arraylist to existing one is made so easy in Kotlin. You can use + operator for this operation instead of calling methods in ArrayList class.
Defining collections is easier than Java in Kotlin. Simply,
Filtering of collections are also made easier in Kotlin. A simple example is given below.
Kotlin will not support static keyword of Java. In a sense, It doesn’t have such a concept. Btw, Companion object of Kotlin will resolve the problem.Those are singleton objects of the current class which will work as a static fashion.
Default param value is a cool concept of Kotlin. You might have did this in Python language. But Java does not support this cool features yet. This concept will allow you to pass the parameter value if it is not null and can avoid if it is null.
This post will be updated with more tricks and tips soon.