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'
importkotlinx.android.synthetic.main.activity_name.*
textView1.text="Sample Text"
-
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)
-
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()
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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