Kyrmyzy Kaliyeva, a Senior Android Engineer at Cartrack (Singapore), and a Kotlin Lover.

Kyrmyzy is one of my best friends who I met in Kuala Lumpur in spring 2017. Since then, I’ve been admiring what she does and the way she learns. It has been 1,5 years when Kyrmyzy started writing on Kotlin. I was curious to know what is important to know when you start a journey named Kotlin World.

Kyrmyzy Kaliyeva - Kotlin

How to migrate to Kotlin?

If you are already comfortable with any language (let’s say Java), I think, the best way of learning a new language (let’s say Kotlin) is by comparing them. First, you will understand the difference between them. Second, it is the easiest way to have a deeper knowledge of both languages and remember it. There are many “Kotlin for Java developers”, “Swift for Java developers” alike courses. Go ahead with them.

What’s the difference?

“How does it work?” – is your fellow traveler. Thanks to Kotlin and Java interoperability. I had the chance to get solid confidence in what I am writing. There are a few examples I would like to share.

simple example of val and var . Here are val uses to declare the immutable variable, and var for the mutable variable. It is not confusing and looks very intuitive at first sight. Let’s ask the question and try to compare it with Java.

Let’s see Java code for Kotlinvar seasonName = "Spring"

private String seasonName = "Spring"

public final String getSeasonName() {
    return this.seasonName;
}

public final void setSeasonName(String name) {
    this.seasonName = name;
}

and for Kotlinval planetName = "Earth"

private final String planetName = "Earth"

public final String getPlanetName() {
    return this.planetName;
}

As we can see, for val there is no setter. That is the only difference which makes val immutable.

There areby lazyand lateinit keywords. And they also feel intuitive aslateinit for late initialization and by lazy for lazy initialization.

Here is Kotlin code for the lateinit keyword:
lateinit var seasonName: Season
and Java code:
public Season seasonName;

public final Season getSeasonName() {
   Season season = this.seasonName;   if(this.seasonName == null) {
       ThrowUninitializedPropertyAccessException("seasonName");
   }   return season;
}

public final void setSeasonName(Season season) {
   this.seasonName = season;
}

Kotlin code for the by lazy keyword:

private val planet: Planet by lazy {
    Planet()
}

and Java code:

private Planet planet;

public synchronized Planet getPlanet() {
    if (planet == null) {
        planet = new Planet();
    }
    return planet;
}

In the code, we can clearly see what are the differences between the two keywords. Keyword synchronized guarantees thread-safety when using by lazy initialization. Which means that the initializer will be invoked only once. Conversely,lateinit represents the multi-threaded environment. Continuing it we can intuitively say that lateinit always goes with var and by lazy with val .

Are there no primitive types in Kotlin? If we look at declarations: Int, Double, Boolean, all of them are wrapper objects for primitive types. When Kotlin code converted to JVM code, wherever possible, these wrapper objects would convert to Java primitive at runtime.

Collections and nullable types never convert to primitive. And, again, it is very intuitive, if you worked with Java.

Kyrmyzy Kaliyeva - Kotlin

What array to use while working with bytes: ByteArray or Array<Byte>? Above examples show that collections do not have primitive types, based on that we can say Kotlin Array<Byte> is an array of objects in Java Byte[] and Kotlin ByteArray is an array of primitive types of bytes byte[].

Read more: How Software Engineer Kyrmyzy Kaliyeva from Almaty found a job in Kuala Lumpur


I hope, these examples showed that having a solid understanding of “what is going on behind curtains” helps to write a stable application with a clean code inside.