Menu Close

Why using Kotlin instead of Java? | #2

First of all: Java is a great language. It is the first programming language I have learned in my university and it is the number one popular (which does not mean it is the worlds best) programming language (by 2016), followed by C, according to the TIOBE Index. So, why should I use a different language than Java? For me it was to learn a different language, and due to the fact that Kotlin has some interesting advantages, I wanted to give it a try. You can create concise and smart code, which should be interesting for every Software Developer.

Hello World in Kotlin:

class Main {
    fun main(args: Array<String>) {
        println("Hello, world!")

Hello World in Java:

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

As you can see, the semantics is quite similar, but the syntax differs between the two languages.

Features of Kotlin

In the following I will show you some features of the Kotlin programming language:

import java.util.*

fun example(from:List<Int>) {
    val to = ArrayList<Int>()
    // 'for'-loops work for Java collections:
    for (item in from)
  • LibGDX supports Kotlin, except for building Web applications: GWT is used to target the web platform and it does not even support Java 1.6. It also needs source files, which is not always affordable. So if you really want to you mobile game to also run at WebGL enabled browsers, you either have to use Java instead of Kotlin or use a different framework than GWT, for example TeaVM. There is a libgdx-teamvm-backend which helps you to compile.
  • Cleaner syntax than Java (There is no new keyword, because it is unnecessary and optional semi-colons).
  • Due to the fact that Kotlin is from JetBrains, the creators of IntelliJ IDEA, the IDE has a integrated “Convert Java to Kotlin” button.
  • Attractive for Android, because Kotlin targets Java 6 transparently, without the same loss of features like you get in Java.
  • Null-safe types (null-pointer exceptions appear at compile-time instead of runtime). The type system handles the null references from code, which is a big advantage of Kotlin against many other programming languages. It distinguishes between references that can hold nullable references (null) and those that can not.
var str: String = "example"
str = null // compilation error, str can not hold null

var b: String? = "example"
b = null // OK, b can hold null

println(a?.substring(0,4)) // prints "exam"
  • Easy use of a singleton:
public object MySingleton {
    public fun example() {}

// Use the singleton

… and many more interesting features.

Try Kotlin online

Go to to try Kotlin online!



Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen + 16 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.