Getting Familiar with Kotlin

Akqeel
2 min readApr 14, 2018

--

So recently I started building an Android app in Kotlin and thought it might be a good idea to list down some of the new features and syntax of the language I came across.

Some of the primary features I enjoyed about developing in Kotlin was one, the code base remained very concise (and easy to read) and two, NullPointer prevention. NullPointers have been regarded as one of the major sources of application crashes, and the ability to identify them at compile time is a big plus. Another important feature Kotlin offers is interoperability with Java, so if you develop for Android then the transition becomes seamless.

This list I hope will be useful when trying to recall the usage of certain features and syntax when getting started with language.

semicolons — Kotlin no longer requires us to end each statement with a semicolon. Yay!

[reference name]:[reference type] — syntax is followed when declaring typed variables.

object creation — to keep code concise and easier to read, Kotlin has taken away the new keyword which was required for object initialization in Java.

nullable? — the reference type of in the decleration of any nullable object must be postfixed with ?, else this variable cannot be assigned a null. NullPointer prevention is one of the major improvements Kotlin provides in terms of safety.

var — any mutable object must be declared using var.

val—an immutable object must be declared using val.

lateinit — we cannot declare variables without initializing them the way we do in Java. If we were to declare uninitialized variables we will need to mark that variable with lateinit.

object — Kotlin provides the Singleton implementation with a single keyword! Just use object in place of the class keyword and Kotlin will produce the Singleton implementation for this class. However, passing initialization parameters for this Singleton is not possible at the moment, and requires a custom implementation.

getters / setters — when we declare a variable, Kotlin automatically generates the getters and setters for that variable.

open — classes in Kotlin are by default final. In order for a class to be extendable it needs to be marked with open.

companion object — any property or function which needs to be shared among all instances of a class can be defined within a companion object. This is similar to the static keyword in Java.

object:[interface] — syntax is used to create new instances of interface classes.

data —Kotlin will generate the equivalent POJO version in Java for any class marked with data.

Hope this list is useful, and I will try to keep updating this as I come across more new features. For further details on Kotlin you can checkout the links below.

Official Site: https://kotlinlang.org

Kotlin and Android: https://developer.android.com/kotlin/index.html

--

--

Akqeel

App developer, on a mission to learn… and share!