A small disclaimer at the beginning: while I have some background in Java, I would never consider myself a superb Java developer. In the recent years I mainly opted for Go to get stuff done, maybe missing out on some of the more interesting programming language trends. This is the main reason I decided to learn Kotlin: dipping around in new concepts. I chose Kotlin because of the recent 2019 Stack Overflow survey. By developers Kotlin is ranked at the 4th rank of most loved programming languages. Since I know Python (mainly for ML use), have learned a bit of Rust (but I found it way to verbose) and written quite a bit of TypeScript in combination with Vue.js web framework; I decided to investigate why exactly Kotlin is so popular.

Getting Kotlin ready

I found out that there is a Kotlin Playground very similar to the Golang Playground. It works pretty well and has an attached workshop site called Koans. If you want to execute it on your own machine, I prefer to use IntelliJ by JetBrains just because the Kotlin support is outstanding (as JetBrains is the company behind Kotlin).

Hello, world!

Because starting to learn a new language always begins with a greeting, we want to build a simple Hello-World program that outputs something to the command line.

fun main(args: Array<String>) {
  println("Hello, World!")
}

As you instantly recognize, the Java heritage is strong in this one. Since main is a static method, we do not have to wrap it with a class, although I think the Kotlin compiler puts it in a wrapper later. You can instantly see one of the great joys of Kotlin, atleast for me as a Go developer: The verbosity of Java is nowhere found. If you do not believe me, this is the same program in Java.

public class Main {
	public static void main(String[] args) {
		System.out.println("Hello, World!");
	}
}

Just by adding some Java objects to the global namespace, Kotlin has done wonders for developer productivity. This also can be seen when you want to do more complex things in Java like counting string occurences.

List<String> myWords = Arrays.asList("hello", "world", "my", "dude", "world");
int count = 0;
for (String word : myWords) {
	if (word.equals("world")) count++;
}
System.out.printf("world occurs %d times.\n", count);

We can use lambda expressions and operations to shrink that block of code massively.

val myWords = listOf("hello", "world", "my", "dude", "world")
val count = myWords.count { it == "world" }
println("world occurs $count times.")

And it’s perfectly readable and understandable. I am very pleased by the use of the equality operator for structural equality therefore testing if the content is the same and not the reference. Don’t worry, if you want to use reference equality there still is the === operator. Also if you do a variable == null check, it automatically gets extended to a variable === null expression.

Data classes

But we are still missing out on the real good stuff, a concept called data classes. Data classes are the true boilerplate killers in Kotlin, at least judging from my experience in Java. A typical Person class in Java with read-only properties would look something like the following:

public class Person {
	private final String firstName;
	private final String lastName;
	
	public Person(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
}

The same, but different in Kotlin looks like this.

data class Person(val firstName: String, val lastName: String)

Voilá, nothing more to it. Same method, same functionality. I begin to grasp why Kotlin is a loved programming language. It feels a lot like Swift but without the Objective-C legacy bullshit.