How Is Kotlin Used In Android?

Launched on Feb 15, 2016, Kotlin Android application is a statically typed programming language which runs on JVM. It has been developed by jet brains. It is quite easy to adopt and encompasses within itself wonderful features. Kotlin is being considered as a boon for the Android developers. One of the reasons is that it is compatible with Java. The aim of Kotlin is to solve the problems that Java had. It has a stable model and a strong commercial support. It has numerous features which will encourage you to adopt it.

Kotlin Android Programming Language

1. Data Class

You can define data class in a single line in Kotlin. For example, we don’t need to create getter setter function explicitly like you wish to in Java.

In Java we create detail class like,

Class game{
String name=””

Public setname(string s)
Pulic String getname()
Return this.name

In Kotlin we will do it as,
data class book ( var name:string)

2. Extension Function

With the help of extension function you can now extend the functionality of an existing class without inheriting that class. Eg.

As in Java ,we inflate the view in adapter,
Override OnCreateViewHolder(ViewGroup :parent,ViewType :int):ViewHolder
val v = LayoutInflater.from(parent.context).inflate(R.layout.view_item, parent, false)
return ViewHolder(v)

In Kotlin we use it this like,

fun ViewGroup.inflate(layoutRes: Int): View {
return LayoutInflater.from(context).inflate(layoutRes, this, false)

3. Smart Cast

We do not have to cast the val explicitly in Kotlin.

if(int is Integer)
return int.length

in java – return(Integer)int.length

4. Type interface

In type interface, one does not have to declare the type of ArrayList
for an arraylist=arraylistof(“kotlin”,”java”,”android”)//

5. Functional Programming

Kotlin has inbuilt functions to perform different operations.

Let’s take a list of numbers
val nonnegative = listfilter(it>=0)

6. Creating Objects

Singleton is a very useful pattern, and Kotlin (after Scale) makes it easy to declare singletons:

object DataProviderManager {
fun registerDataProvider(provider: DataProvider)
{ // …


val all DataProviders: Collection get() = // … }

This is called an object declaration, and it always has a name following the object keyword. Just like a variable declaration, an object declaration is not an expression, and cannot be used on the right-hand side of an assignment statement.

To refer to the object, we use its name directly:

7. Default Argument

In Kotlin there is a powerful feature of default argument, as the name describe we can put arguments in a function with default values, and how the Kotlin treat with these function call is described as follows,
class Book{

fun update(title: String, subtitle : String = “No Subtitle”, abridged : Boolean = false){

In Kotlin we can call this function,
book.update(“Josef rosef”)// subtitle and abridged with default values or we can put the parameter we required

book. update(“tajmahal”,abridged=true)//subtitle with default

8. Named Argument

To use named parameters, we put the parameter name before the argument value. Here is the function call again, this time with named parameters:
reformat(str, normalizeCase = true,
upperCaseFirstLetter = true,
divideByCamelHumps = false,
wordSeparator = ‘_’ )

we can call this like,


9. Declaration

We can declare variable without giving its type to the compiler.
val a :int=1// immediate assign
val b=2 // int infered
c=3 //deferred assignment
fun PrintSum(a:Int,b:Int)
println(“sum of $a and $6 is {a+b}”)
fun double (x:int):Int=x*2

10. Null Safe

One of the most common pitfalls in many programming languages, including Java, is that of accessing a member of a null reference, resulting in a null reference exception. In Java, this would be the equivalent of a Null Pointer Exception or NPE for short. Kotlin’s type system is aimed to eliminate Null Pointer Exception ‘s from our code.

The Only Possible Causes

In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can’t (non-null references). For example, a regular variable of type String can’t hold null:
var a: String = “ABC”
a = null // compilation error
To allow nulls, we can declare a variable as nullable string, written String?:
var b: String? = “abc”
b = null // ok

Why Kotlin?

Kotlin is good because it compiles to Javascript. It has been designed to solve problems of programmers existing in today’s world. Along with it, it is open-source. Java projects can be converted easily and the programs can still be compiled. Even the complex programs can be converted easily. Moreover, it uses existing Java frameworks and libraries. It distinguishes between nullable and non-nullable data type. It does not involve an option type and is zero overhead.

Kotlin can be used easily by the Android application developers as it has been developed for them. It can be learned in very less time. It does not impose any runtime overhead. Moreover, its dependency on Java makes it easier. Kotlin compiler can understand from the code written by a developer and can develop the rest of the code itself. This is not the case with Java. One has to write all the codes which are quite a time taking. Kotlin is being backed by well-established companies. It has a team constantly working on it. It does not involve the risk of getting abandoned. It also does not involve the risk of runtime.

Kotlin is not very much new. It has been out there for some time and has been adopted by many companies. Kotlin has been accepted positively throughout as it has solved the difficulties that Java had. Rise from the negative points, take the risk and adopt Kotlin to prepare yourself for the upcoming challenges.

About Sanyukta

No Comments

Leave a Comment

Location Map

Request a Quote

Copyright © 2012-2017 All Rights Reserved at Trion Technologies.

Design and Developed by Trion Technologies