Kotlin The Future Of Android Development

An Introduction
Until recent past, Android app development was extensively done through the Java programming language and Java 6, which came in existence since 2006, two years before the release of Android devices.

Kotlin language is advent by JetBrains in 2011.As soon as did Kotlin reached at 1.0 status early in 2016 then at Google I/O 2017 in April, Google announced that Kotlin would be forever supported as a first class programming language for Android development. Moreover, the latest released of Android Studio 3.0 also supports Kotlin.

Kotlin is first statically typed and cross-platform language with out of box type interference which will ease developers to create or develop application for multiple platforms.

Why Kotlin Not Java For Android?
Java is most preferable among few other altenatives like:-C++,Lua etc but Kotlin is growing at a significant rate and majority of programmers are switching from Java to Kotlin.

Some of the reasons are:-

Similar Syntax
The syntax of kotlin language is very familiar and easily graspable for person who has good amount of grip in OOPs concepts. But still there are some minor syntax difference compare to Java.

For Example:-

Function in Kotlin 

fun foo() { 
   val test = object { 
       var x: Int = 0 
       var y: Int = 0 
   } 
   print(test .x + test.y) 
} 

Variable Declaration In Kotlin

var x: Int = 0

Code Concise

Compare to Java, Kotlin uses very less number of lines and also reduces the Boilerplate Code which developer need to rewrite.

For Example:-

In Kotlin

class User(id: Int, name: String, age: Int) { 
   var id: Int = id 
   var name: String = name 
   var age: Int = age 
}

In Java

public class User { 
   private int id; 
   private String name; 
   private int age; 
   public User(int id, String name, int age) { 
       this.id = id; 
       this.name = name; 
       this.age = age; 
   } 
   public int getId() { 
       return id; 
   } 
   public void setId(int id) { 
       this.id = id; 
   } 
   public String getName() { 
       return name; 
   } 
   public void setName(String name) { 
       this.name = name; 
   } 
    public void setSurname(String surname) { 
       this.surname = surname; 
   } 
   public int getAge() { 
       return age; 
   } 
   public void setAge(int age) { 
       this.age = age; 
   } 
}

Ease Of Learning

Being a programmer-friendly language, learning and understanding the Kotlin code does not need much time. One can easily and quickly learn it with an expenditure of few’s hours because of Jetbrains, as they are very much concerned over transition to kotlin from Java.

Null Safety
It is the main advantage of Kotlin over Java as it eradicate “The Billion Dollar Mistake” which is “null references”.Not only in Java, but also other programming languages has same pitfall which result in “null reference exception or NullPointerException”.

Kotlin could never throw a NullPointerException unless you ask for it, as it support nullability as part of its type system. So, you have the control or power to decide whether  a variable can hold a null value or not. Hence, the compiler can detect possibility of NullPointerException errors either at compile time or runtime.

Let’s understand how it works!

  1. Adding a null Check

The most inconsequential way to handle nullable variables is null check before accessing.

For Example:-

 val b: String? = "Kotlin"
if (b != null && b.length > 0) {
    print("String of length ${b.length}")
} else {
    print("Empty string")
}
  1. Safe Calls

Null check for every nullable reference could result in cumbersome. Luckily, Kotlin has a syntax which is called Safe call operator “?.” which perform null-check and a method call in a single expression.Thus, it allows programmer to perform specific action or operation for non null variables.

For Example:-

val PersonName: String? = null

println(PersonName?.toUpperCase())
println(PersonName?.length)

// Output
null
null

Output is “null” since the variable “PersonName” is null, otherwise, it would have printed name and length in uppercase.

So, using safe call operator “let”, you can perform any operation like do not print anything if variable is null.

For Example:-
val PersonName: String? = null
PersonName?.let { println(it.toUpperCase()) }
PersonName?.let { println(it.length) }

// Prints nothing
So, the lambda expression inside “let” only execute if and only the variable “ PersonName” is not null.

Even you can have chain safe calls

  1. Elvis operator

The Elvis operator “?:” is used to return a default value from operation if the references hold null value.

For Example:-
val name = PersonName?: “Guest”

In another words, Elvis operator will return the actual value of variable “PersonName” if it is not null, otherwise it will return second value which is default.

  1. Not null assertion or !! Operator

The !! operator converts any null value to a non null value, and throws a “NullPointerExceptionif the nullable type holds a null value.

For Example:-

val nullableName: String? = null
nullableName!!.toUpperCase() // Results in NullPointerException