Why Kotlin?

Jamian
4 min readJan 18, 2019

Well, within my talks, to a lot of Android developers, more often than a couple of times, I’ve come across the question, why Kotlin?

Now if you’ve asked anyone who’s worked with Java, and now moved to Kotlin, for a comparison, the usual answer would be something like, driving a Fiat that's now upgraded to a Range Rover. Yes, Kotlin is powerful, and I’m going to talk about a few features that I love about it!

Statically typed language
To begin with, Kotlin is a statically typed language that supports type inference. Woah! To break that down, Kotlin checks the type of variable at compile time, rather than at run-time, making it easy to catch errors before a successful compilation. It also, automatically detects the data type of variables avoiding explicit variable type declaration.

var justAnotherString = "Hello, little Toe"
val justAnotherInteger = 27

Extension functions
This is one of my faves. Kotlin will allow you to extend the functionality of classes without touching their code. For example, we can add power to a humble String class by just adding extension functions to it,

fun String.showToast(context:Context){
Toast.makeText(context, this, Toast.LENGTH_SHORT).show()
}

with which you will be able to add a showToast() function to any String, just by adding a showToast() function that takes a parameter of context.

"Hey there!".showToast(this)

Extension functions extends way beyond just displaying toasts, but I’ll discuss that in another blog.

Data classes

One of the most common things I’ve done is to create custom classes in Java also called POJO, to store and retrieve data. It basically meant creating a class with a bunch of variables, getters and setters. It looked something like this.

class Person{
String name;
int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

In Kotlin, you can achieve the same thing, by writing something like this.

data class Person(val name: String, val age: Int)

Data classes in Kotlin help encapsulate a lot of boilerplate code and allows you to create objects that hold data, have properties of getters and setters, and and a few more things, like generating a hashcode or a copy of that class.

No more findViewById

findViewById is something that everyone coding in Android has used. Well, not if you’re coding apps in Kotlin. With Android extensions you can completely get rid of the findViewById method. All you need to do is all this line in your gradle file.

apply plugin: 'kotlin-android-extensions'

now within your onCreate method you can reference variable directly. You don’t need to explicitly declare the view nor create global variables any more.

Say your xml file had a view like this,

<TextView
android:id="@+id/tv_name"
android:text="Rahul"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_profession"
android:text="Guitarist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

all you would need to access and edit it would be as follows

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

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_name.text = "Simran"
tv_profession.text = "Android Developer"
//And thats all!
}

}

Null safety

Kotlin fights the NPE (Null pointer exception) battle real hard. By default, Kotlin will not allow a null value in any variable.

var name:String = null //this will not compile

To allow a null value in a variable you need to add a ? operator after the variable declaration. Something like this.

var name:String? = null

So the next time you try to access, or pass in the value of name variable you will have to do something like this

println(name?.length) //this will print 'null'
println(name!!.length) //this will throw a NPE exception

using the ?. operator, aka the safe call operator, Kotlin handles the NPE exception and returns null to the logs.

the !! not null assertion operator, will throw a NPE exception, just like good ‘ol Java would. This is for the people who still want NPE to be a part of their life.

Another operator, called Elvis, ?: is also used for null safety. The Elvis operator will analyse a variable and return a default value if the variable holds a null value.

var name:String = null
println(name?.length ?: -1) //this will print -1
name = "Susanne"
println(name?.length ?: -1) //this will print 7

Kotlin has a lot more features that have made the life of Android developers much easier, I will try to talk about a few more in detail, in my upcoming posts.

Cheers.

--

--