Why Kotlin?

For the past few years, I have been coding Android applications with Java and I’ve loved it. Java is my favorite programming language and I still remember the days I started learning Java. But then one day Kotlin comes along and catches my eyes! It looks like Swift too and I was excited like any other Android developer would be!

Java is an old language, I mean really old. In case of Android, Java has not caught up with “modernization” and I doubt that it ever will. Before you say “Wait, whaaat?” keep reading :P

Higher Order Functions or Lamdas

higher order meme

Let me quote a few lines from the Android Developer site

Android supports all Java 7 language features and a subset of Java 8 language features that vary by platform version..

Ok, So Java 8 is supported partially? But it was released 2 years ago. Kotlin 1.0 was released last year (2016) and in this year’s Google I/O (2017), Google announced first-class support for Kotlin on Android!

That being said Kotlin has functional programming baked right in. Kotlin provides all essential functional facilities such as filter, map & flatMap, take & drop first & last, fold & foldRight, forEach, reduce, and anything else the pragmatic functional programmer could ask for!

A Simple example:

val states = listOf("Mashed", "Fried", "Potato", "Sunny Side Up")
val myKindOfPotatoe: Iterable<String> = states.filter { it.length <= 7 }.map { it + " Potato" }
println(myKindOfPotatoe)  // [Mashed Potato, Fried Potato, Potato Potato]

Diving deeper into this, in the future.

Write less code

We write less code with Kotlin. When we create a Java POJO or a Model class for storing data, It comes with variable declarations, their setters, their getters and what not!

But behold the data classes in Kotlin.

data class Book(val name: String, val authorName:String, val cost:double, val publication:String)

With Just one line of Code, you get all the setters, getters and the other Java Functions viz., toString( ) and copy( ) function for free!

Extension functions <3

We can now extend the functionality of a class without touching its code or inheriting it! Let me show you how powerful this can be:

fun Int.Square(): Int {
  return this * this
}
//Now we call it
var squareOf9 = 9.Square()

Need another example? Aite!

fun ImageView.load(url: String){
  //your logic for downloading the image 
  //        Or
  Glide.with(context).load(url).into(this)
}
//Now we use it like
imageView.load(url)

Make existing classes great again!

No more Null pointer Exceptions

Well, this is a no-brainer but Java is known and made fun of its NULL POINTER EXCEPTIONS. But this ain’t the case with Kotlin, Kotlin provides Null safety through nullable and non-nullable types, safe calls and safe casts . Kotlin forces you to initalize a variable while declaration or makes you declare a variable as optional (Meaning it may or may not hold a value)

 var nonNullableVar: String = "My string" // needs to be initialized
 var nullableVar: String?
 

Now you can check if the variable is null in 2 ways:

 if (nullableVar != null)  // The Java Way
  nullableVar.doStuff()
 //OR
 nullableVar?.doStuff() // *cough* *cough*
 

And other features like Named Arguments, open classes and a lot more which we will explore in my future blog posts.

Reference, Kotlin docs.