Kotlin for Android is quite the rage now. Some of you might still be new to the idea, why try Kotlin? Why switch to Kotlin language for Android app development? Why choose Kotlin programming language over Java?

Our pick of top 10 reasons to choose Kotlin for Android app development might just make up your mind to switch over to Kotlin app development.

1: Easy to setup

Android studio 3.0 offers full support for programming in Kotlin. That means you can create Android applications using Kotlin with just a few clicks. Simply create new projects as you did previously and check the box labelled ‘Include Kotlin Support’. It’s that easy to Setup.

2: Interoperability with Java

Ah! The forte of Kotlin app development. Kotlin code is fully interoperable with Java code. Allowing you to mix and match code with Java and Kotlin. You won’t need to convert all Java code JUST to work with Kotlin and vice versa. It’ll be your choice. You can call Kotlin code from Java and Java code from Kotlin. Even if you have as little sense of programming as a monkey does of peeling bananas, you will do just fine.

3: Boilerplate Code

Raise hands if you like writing lengthy lines of code? No one, right?

This is another treat Kotlin coding language brings to us. Boilerplate code is such code that you need to write again and again with no alterations. Kotlin reduces that to almost 20%. Code less, Debug less.

4: Synthetic Extension

I am covering this here because this one is one of the classic examples of how boilerplate code is reduced in Kotlin language applications. Kotlin offers many extensions and plugins to make your android programming life easy. One of those is the Synthetic Extension. Using this, you can completely access views from the XML layout files without having to write ‘findViewById’ again and again. One less headache right!

This is how you work with views in Kotlin

// Using R.layout.activity_main from the ‘main’ source set

import kotlinx.android.synthetic.main.activity_main.*

class MyActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

// Instead of writing findViewById<TextView>(R.id.textView), you write
        textView.setText(“Hello, world!”)

    }

}

5: Choice between OOP & FP

This one is for untraditional coders and programmers and developers.

Those of you with a Functional Programming background can enjoy working with Kotlin language in Android as much as the ones with an Object Oriented programming style. Yup, you heard me, Kotlin supports both styles and leaves the choice to you. So FP or OOP which one are you going to try in Kotlin?

6: Null Pointer Exception

The billion dollar mistake ‘NPE’. If you haven’t encountered one, then you’re not an android app developer yet or an exceptional prodigy (hardly likely though). Kotlin language has a type system that nullifies the risk of operating on Null references thus avoiding the NULL POINTER EXCEPTION altogether. Hurray to never seeing an NPE!

However, if you need to work with Nullable types here is how you do it in Android Studio using Kotlin.

This function should return null if the variable “str” does not hold an integer value:

fun parsingInt(str: String): Int? {

    // …

}

Using a function returning a nullable value:

fun printValue(arg1: String, arg2: String) {

    val x = parseInt(arg1)

    val y = parseInt(arg2)

// Using `x * y` will yield an error because they both may hold null values. So,

    if (x != null && y != null) {

        // By using this null check x and y will automatically cast to non-nullable

        println(x * y)

    }

    else {

        println(“either ‘$arg1’ or ‘$arg2’ is not a number”)

    }   

}

7: Choice of Semicolons

Optional semicolons as line terminators is also another feature in Kotlin. No restrictions, if you like using them you can, if you don’t, you can choose not to use them. Kotlin code will work with both choices.

8: Lambda Expressions are supported

Another powerful feature by Kotlin language in Android app development. Lambdas significantly reduce the complexity of functions. In simple terms, Lambdas are chunks of code that you can pass to other functions. This also decreases code verbosity. Although Java 8 does provide some support for this feature, but not as efficiently as Kotlin language in Android studio does.

This is a Lambda function in Kotlin

max(strings, { a, b -> a.length < b.length })

which would be equal to writing a normal functions as

fun compare(a: String, b: String): Boolean = a.length < b.length

9: No cost to adoption

Apart from the hesitation of trying a new language, there is no cost of adopting Kotlin language. Why? Because Kotlin is an open source language. It is completely free and available for everyone to use. If you have old Java projects, nothing to fret about. Simply use the Java-to-Kotlin converter plug-in and all Java code will be converted into Kotlin code. Your file  size and code complexity will also reduce.

10: Commercial Language

Kotlin was developed by a software firm, the Jet Brains. That makes Kotlin a commercial language. Now the benefit that comes with that is: Kotlin is designed according to real-life problems of modern-day developers and programmers. It includes the pros of a modern coding language and addresses the problems faced by programmers in the industry. You will notice a rising trend of IT titans who are using Kotlin for their applications.

Conclusion:

Kotlin is here to stay and everyone still on Java will soon need to pack up. If you need more convincing check out our article on Kotlin vs Scala. Every language has its strengths and weaknesses, and I don’t mean to say there is something not right with Java only that ‘Java is archaic’. So its time you move out with the old and in with the new.

Get in touch with us to get your very own Kotlin Android application now.