Tutorials  Language  Kotlin
Kotlin

First class support throughout

Hardcore Android developers have been aware Koltin could be used alongside Java for some time, but many have been held back by the lack of official support. That ended at Google I/O 2017, when it was announced it will now be fuly supported from Android Studio 3.0 onwards, and that's right through from the IDE syntax checkers to the Gradle build system and beyond.

How has Kotlin been implemented on Android?

Since Android is powered at it's heart by a JVM, that's Java Virtual Machine, in theory, anything which can create the bytecode it expects can be used to run software on it. Koltin is such a beast, and AS 3.0 now has the toolchain needed to generate it. So it's a hat tip to the designers of the original JVM for creating a system with such longevity.

Uniquely, Kotlin is not an "all or nothing" swap in for Java, the way other languages would be (or at least not without a few very complicated hoops to jump through). It actually sits side by side in the source tree and is even interoperable with Java, so it can call Java methods directly and vice-versa. Migrating existing projects over just lost what is traditionally the largest hurdle they face.

Why Kotlin?

It's no accident Kotlin has been dubbed "The next Java". In a nutshell, it retains all the best features of Java whilst adding many innovations of it's own, which came from the world of development, since Java first appeared in 1996. It incorporates features from other languages - such as Scala - which have been proven to boost productivity, improve readability and reduce development times. It's open source - that's crucial for mass adoption today, and as we all know, without a critical mass you face the "wait for the others first" issue many new languages become embroiled in. It's 100% backed by Google, and is now an integral part of the main tool they offer which almost every Android developer relies on - Android Studio.

What does it look like?

You'd be forgiven for thinking you're looking at regular java at first glance:


/**
 * In this example, `val` denotes a declaration of a read-only local variable,
 * that is assigned an pattern matching expression.
 * See http://kotlinlang.org/docs/reference/control-flow.html#when-expression
 */

fun main(args: Array) {
    val language = if (args.size == 0) "EN" else args[0]
    println(when (language) {
        "EN" -> "Hello!"
        "FR" -> "Salut!"
        "IT" -> "Ciao!"
        else -> "Sorry, I can't greet you in $language yet"
    })
}

A couple of things jump out right away - no semicolons (yay!), some new method qualifiers ("fun") - but generally its emphasis on readability is directly built on the work which also went into that for java. This means it's also really quick to learn.

Language details

Many adopters become evangelists. Phrases like "its nicer" and "more pleasant to use" are often heard from these types. Here's a few highlights:

1 Null safety

NPE's are simply not thrown unless you go out of your way to cause them. It's done at a fundamental level, so for example if you create a String such as var a: String = "abc",  you cannot then write a = null because the compiler will catch it.

2 Leaner syntax

Types are inferred, so don't need to be explicitly stated, objects have real properties so there's no more cumbersome get/set methods. Often it makes sense to have a function outside a class, again something Java forbids.

3 All exceptions are unchecked

How much clutter do typical Java programs have when developers are forced to surround library code which can throw an Exception with try ... catch blocks? Often these are useful, but every serious Java developer has encountered the "I'll have to put an empty catch block here, even though I know it's wrong" issue.

4 Functional programming

Lambdas and code folding over collections are innovations which have now been proven, so are incorporated at the core.

5 Extension functions

You can add methods to classes without modifying the source code for them. You still have the inheritance mechanism familiar from Java, which you'd have to use to add a method to an existing class. Now you can do it directly, i.e. the class name stays the same!

Take a look at Googles announcement at this years' I/O: