Getting started with Kotlin

If you are into Android Developerment or just started learning Android Development I bet you heard about Kotlin.

Lets see what is the definition of Kotlin ?

Kotlin is an open-source, statically-typed programming language that supports both object-oriented and functional programming. Kotlin provides similar syntax and concepts from other languages, including C#, Java, and Scala, among many others. Kotlin does not aim to be unique—instead, it draws inspiration from decades of language development. It exists in variants that target the JVM (Kotlin/JVM), JavaScript (Kotlin/JS), and native code (Kotlin/Native).

What above paragraph tells us about Kotlin is that Kotlin is neither new programming language nor it provides some new discovery in Software engineering.

Kotlin is just an easier way to make things happen. The Kotlin is a new religion where the first disciple is google. Google is pushing Kotlin and making every native Android developer to follow it too.

This new programming religion is providing ease to disciples by removing boiler plate code

Create a POJO with getters, setters, equals(), hashCode(), toString() and copy() in a single line:

data class Customer(val name: String, val email: String, val company: String)

Or filter a list using a lambda expression:

val positiveNumbers = list.filter { it > 0 }

Want a singleton? Create an object:

object ThisIsASingleton {
    val companyName: String = "JetBrains"
}

This is a piece of good news for people who want to finish their projects on time without wasting time on unproductive things and bad news for those who get paid on an hourly basis. I cried every time I created data classes.

When I was learning Android Development I faced NullPointerException very often and I used to plug my hair whenever I had to face that.

While scratching my bald scalp I read that Kotlin protects you from mistakenly operating on nullable types gave me hope to get a hair transplant.

Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake

var output: String
output = null   // Compilation error

Kotlin protects you from mistakenly operating on nullable types

val name: String? = null    // Nullable type
println(name.length())      // Compilation error

And if you check a type is right, the compiler will auto-cast it for you

fun calculateTotal(obj: Any) {
    if (obj is Invoice)
        obj.calculateTotal()
}

Kotlin is managed by the Kotlin Foundation, a group created by JetBrains and Google, that is tasked with advancing and continuing development of the language. Kotlin is officially supported by Google for Android development, meaning that Android documentation and tooling is designed with Kotlin in mind.

Certain Android APIs, like Android KTX, are Kotlin-specific, but most are written in Java and can be called from either Java or Kotlin. Kotlin’s interoperability with Java is core to its growth. It means that you can call into Java code from Kotlin and vice-versa, leveraging all of your existing Java libraries. Kotlin’s popularity results in a nicer development experience on Android, but development of the Android framework continues with both Kotlin and Java in mind.

Kotlin’s interoperability with Java means that you don’t have to adopt Kotlin all at once. You can have projects with both Kotlin and Java code.

About the author

admin

I am GD, I am full time Android Developer and Machine Learning Enthusiast. I like to learn and try my hands on new technologies and programming languages.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *