Panduka Wedisinghe
3 min readJun 3, 2018

--

KOTLIN for Android

Java has been used as the main programming language for android application development since its beginning.Do you think Java is the only option we are left with?

Answer is NO. You can write applications for Android by using any programming language which can be compiled and run on Java Virtual Machine also known as JVM. JetBrains introduced Kotlin which supports android application development. In Google I/O 2017, Google announced Kotlin as another official language for android application development.

Most people think Kotlin is the replacement of Java. But it is not the case. Kotlin and Java are interoperable. Both can be used in the same same project. Even Java code can be translated into Kotlin and vise versa.

What is Kotlin?

Kotlin is a Statically Typed Programming Language which is run on JVM. In a Statically Typed Programming Language type checking is done at compile time. Furthermore variables need not be defined before they are used. Java, C and C++ are the prime examples.

Why Kotlin?

Some of the Key features Kotlin having are listed down below.

  1. Concise

Drastically reduces the amount of code need to be written.

Create a POJO with getters, setters, equals(), hashCode(), toString() and copy() in a single line:

data class Customer(val name: String, val email: String, val company: String)

Or filter a list using a lambda expression:

val positiveNumbers = list.filter { it > 0 }

Want a singleton? Create an object:

object ThisIsASingleton {
val companyName: String = "JetBrains"
}

2. Safe

Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake

var output: String
output = null // Compilation error

Kotlin protects you from mistakenly operating on nullable types

val name: String? = null    // Nullable type
println(name.length()) // Compilation error

And if you check a type is right, the compiler will auto-cast it for you

fun calculateTotal(obj: Any) {
if (obj is Invoice)
obj.calculateTotal()
}

3. Interoperable

Use any existing library on the JVM, as there’s 100% compatibility, including SAM support.

import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
Flowable
.fromCallable {
Thread.sleep(1000) // imitate expensive computation
"Done"
}
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single())
.subscribe(::println, Throwable::printStackTrace)

Target either the JVM or JavaScript. Write code in Kotlin and decide where you want to deploy to

import kotlin.browser.windowfun onLoad() {
window.document.body!!.innerHTML += "<br/>Hello, Kotlin!"
}

4. Tooling

A language needs tooling and at JetBrains, it’s what we do best!

Benefits of Kotlin over Java for Android Platform

As we already know Kotlin is not the replacement of Java. Java and Kotling together give us a more powerful language. That is the prime reason android development platforms let developers use both languages. But there are significant advantageous of using Kotlin instead of Java for Android Application Development. Some of them are listed below.

  1. Safer than Java

NullPointerException aka The Billion Dollar Mistake in java is a nightmare for developers. As far as I know almost all the developers hate NullPointerExceptions. But you don’t need to worry about it anymore if you are switching to use Kotlin instead of Java. Kotlin handles NPE by distinguishing between references that can hold null (null-able references) and those that can not (non-null references). For example, a regular variable of type String can not hold null.

2. Easy to learn

Because of the Simplicity its really easy to learn the language faster. Whoever familiar with java can understand the code written in Kotlin, even without having written a single line of code.

--

--

Panduka Wedisinghe

Android Developer /Software Engineer/Flutter Developer