Features that will make you want to switch to Kotlin for Android

Posted By : Daljeet Singh | 05-Jan-2018

Kotlin is a statically typed language which can be run on the JVM and can also be compiled into source code for Javascript.With Kotlin becoming an officially supported language for android development using Android Studio,let's have a look at some of its most exciting features:

  • Safety from Null Pointer Exception : This is one of the most well documented and discussed advantage of using Kotlin for development,it helps you get rid of the dreaded Null Pointer Exception in your app.Every object or variable declared in Kotlin is non-nullable unless we explicitly declare it to be nullable.We must use the question mark("?") operator to declare a variable or value as nullable in Kotlin.

  • Ease of variable declaration : Although Kotlin is statically typed,you can just declare a variable using the "var" keyword and the type of the variable will be automatically inferred from the context in which it is used.Kotlin lets you declare the type of a variable explicitly as well at the time of initialization.

  • Kotlin Android Extensions : Using Kotlin Android Extensions in our app we can use views directly without the need to bind them to objects.You need to add the line:

    apply plugin: 'kotlin-android-extensions'
    
    in the build.gradle of your app module and :
    importkotlinx.android.synthetic.main.activity_name.*
    
    in your activity where activity_name is the name of the layout set for your activity. Afterwards you can just write :
    textView1.text="Sample Text"
    
    to set text,where textView1 is the id of the textview in the activity layout.
  • Data Classes : We can use Data Classes in Kotlin instead of creating POJO classes to hold data in order to reduce the line of codes by a great deal.We can define a class "User" that holds a user's name and id by the following line of code in Kotlin:

    data class User(val name: String, val uid: Int)
    
    The getters ,setters ,equals() and hashcode() functions are automatically derived by the compiler for the above written data class.

     

  • Easier null/non-null checks : Kotlin also provides us with easier alternatives to check for null and non null values in the application. Using the Elvis operator("?:"),we can specify an alternative value for an object in case the object is null.For instance :

    Toast.makeText(context,str?:"No value found.",Toast.LENGTH_SHORT).show()
    
    shows "No value found." if str is null. We can use "let" to perform an operation based on the condition that the object is not null.E.g :
    str?.let{
    Toast.makeText(context,str,Toast.LENGTH_SHORT).show()
    } 
    

     

Apart from these ,Kotlin provides a host of other features such as smart casting,lazy initialization , using "when" in place of switch case or the the if,else if,else block etc.,which help make code more concise and readable.

About Author

Author Image
Daljeet Singh

Daljeet has experience developing android applications across a range of domains such as Cryptocurrency, Travel & Hotel Booking, Video Streaming and e-commerce. In his free time, he can be found playing/watching a game of football or reading up on either

Request for Proposal

Name is required

Comment is required

Sending message..