Have no fear, Kotlin is here — a few notes on Kotlin programming language

google.com

Life is great and everything will be ok, Kotlin is here”- claimed Christina Lee and Jake Wharton during their talk on Google I/O 2017 conference. Kotlin programming language seemed to be one of the widest discussed topics during and after the conference. Mobile technologies market is very dynamic, and it seems that introduction of the new officially supported by Google language will change a lot the way we develop Android apps in next years.

Despite being created only 6 years ago, Kotlin has already made some commotion. Statically-typed, object-oriented, yet using all the sugar of functional programming, it seems to be a very promising alternative for Java 7- based Android platform. As this new programming language from JetBrains is gaining more and more popularity, after Google I/O 2017 we can be sure that Kotlin is here to stay and (as some say) maybe even replace Java in Android development.

Let’s go through some examples of Kotlin features and compare it to Java and other popular languages. Firstly, it has to be mentioned that Android Studio 3.0 has got Kotlin support bundled, and if you want to use Kotlin in AS 2.0 or above you just need to install Kotlin Plugin.

kotlinlang.org

Great thing is that you can use Java and Kotlin classes together (as Kotlin code is compiled to Java bytecode), so transferring your project from one to another or simply taking advantage of using both languages could not be simpler. Plugin also enables you to automatically convert Java files to Kotlin files.

The first visible difference we can see when creating a new class. Similarly to Scala, we can save many lines of code by creating a primary constructor with public properties- all the needed getters and setters are generated for us, with no single line of code.

Hero class in Java:

public class Hero {
private String name;
private int level;
private int maxHealthPoints;
private int currentHealthPoints;
private Weapon weapon;
public Hero(String name, int level, int maxHealthPoints, int currentHealthPoints, Weapon weapon) {
this.name = name;
this.level = level;
this.maxHealthPoints = maxHealthPoints;
this.currentHealthPoints = currentHealthPoints;
this.weapon = weapon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getMaxHealthPoints() {
return maxHealthPoints;
}
public void setMaxHealthPoints(int maxHealthPoints) {
this.maxHealthPoints = maxHealthPoints;
}
public int getCurrentHealthPoints() {
return currentHealthPoints;
}
public void setCurrentHealthPoints(int currentHealthPoints) {
this.currentHealthPoints = currentHealthPoints;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
}
Corresponding code in Kotlin:
class HeroKotlin(var name: String, level: Int, var maxHealthPoints: Int,
var currentHealthPoints: Int, var weapon: Weapon)

The more I dug into Kotlin, the more time-saving features I saw, and the more I thought it looked very similar to Swift. If you need to customize getters/setters, there is a mechanism that you may know from Swift as stored/computed properties (properties with/without backing fields). What is more, in Kotlin code we can access all these properties directly without the need to call get/set, and suitable methods are called underneath.

var level: Int = level
get() = field
set(value) {
// things to do
field = value
}
val healthPercent: Float
get() = (currentHealthPoints / maxHealthPoints) * 100F
val hero: HeroKotlin = HeroKotlin("Sarevok", 3, 40, 30, Weapon("Skull Crusher", "sword", 10))
hero.currentHealthPoints = 35
hero.level = 4
var health = hero.healthPercent

The next feature that Kotlin and Swift have in common is Null Safety. Kotlin introduces separate types for values that may contain null, to make developers aware of problems related to null pointer references. In Android Studio there already is a tool to help with that awareness- @Nullable and @NotNull annotations- but these cause only warnings, and in Kotlin if you want to assign null to not-null variable, you immediately get an error. Basically, there are only a few ways to get NullPointerException in Kotlin, and most of them are to explicitly unwrap null variable (ie. hero.weapon!!.attack()) or when calling Kotlin from Java code. So many hours typically wasted on debugging saved.

In Hero classes presented above Java would let us call

hero.setWeapon(null);	// this assigment could lead to NullPointerException

but in Kotlin

hero.weapon = null

would produce a compile-time error. To make it nullable we should declare var weapon: Weapon?. A great way of handling nullable variables is Elvis Operator: after ‘?:’ we put alternative value if the variable is null:

var heroLevel = hero?.level ?: 1

Kotlin contains some more features that Java used in the Android platform does not support (without use of external libraries) as lambda expressions, extension functions, operator overloading, type inference, smart casts, string templates (string interpolation), or trailing closures- all of them make it more similar to Swift than Java from the visual side. Describing all of them is a material for a separate long article, and if you are interested I encourage you to check out the docs (https://kotlinlang.org/docs/reference/). My main point is to give you a brief overview on how many interesting things Kotlin has to offer.

venturebeat.com

There are also some great things dedicated for android development. One of them is Kotlin Android Extensions plugin. If you ever were tired of using findViewById for binding each view in your Activity, and praised God/Jake Wharton for ButterKnife, you will love this tool. By adding

apply plugin: ‘kotlin-android-extensions’ to your build.gradle file you get a property-like access to all your views without the need of binding them yourself. No more boilerplate code!

androidcentral.com

The amount of Kotlin enthusiasts increases every day. Programmers appreciate its functionality, simplicity, and great support. Moreover, as Kotlin and Swift are very similar syntactically, it is easier for programmers and support teams to cooperate, and in general to develop apps for both platforms as bunch of code can be reused.

Currently Java is (and probably will be, at least for a couple of years) the most popular language for Android development, but it is not unlikely that Kotlin will gain so much popularity, that in a few years a greater part of Android applications will be written in this neat language. As for now, my first few dates with Kotlin went really well, and I see some chances for a longer relationship.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.