Why Kotlin?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
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:
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.
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:
Need another example? Aite!
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)
Now you can check if the variable is null in 2 ways:
And other features like Named Arguments, open classes and a lot more which we will explore in my future blog posts.
Reference, Kotlin docs.