Kontakt

Hello, Kotlin!

Posted on Mittwoch, 25th Januar, 2012

Last year JetBrains announced Kotlin – a statically-typed JVM-targeted programming language. The web-based Kotlin preview has been launched recently. Check out this demo to get the first impression.

I got curious about the Kotlin programming language and attended a talk by Andrey Breslav at Devoxx 2011. Today I’m one of the lucky guys who got the access to the private EAP. After experimenting with Kotlin I decided to write a series of articles about this language in order to share with you my experience. This article is the first one and covers a basic “Hello, world” example. Let’s dive in.

Initial Java program

First we will create a Java program that we later convert into Kotlin. Here is our “Hello, world!” example in Java.

package hello;

public class HelloWorld {
    private String name;

    public HelloWorld(String name) {
        this.name = name;
    }

    public String greet() {
        return "Hello, " + name;
    }

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld("Kotlin");

        System.out.println(helloWorld.greet());
    }
}

An equivalent Kotlin program

Now let’s convert this example into a Kotlin program.

package hello

class HelloWorld(var name: String){

    fun greet(): String {
        return "Hello, $name"
    }
}

fun main(args: Array<String>) {
    var helloWorld = HelloWorld("Kotlin")

    System.out?.println(helloWorld.greet())
}

This tiny Kotlin program demonstrates a couple of Kotlin features. Let’s start with the constructor.

Primary constructors

In Kotlin you may declare constructors immediately in the class declaration. Note that the constructor in the example above initializes class properties without having a body. Such constructors are called primary constructors.

Properties

Do properties in Kotlin have the same meaning as in Java? In contrast to Java, Kotlin classes don’t have fields that are read/written using getters and setters. Instead Kotlin classes have properties: mutable properties are declared with the var (for variable) keyword and read-only properties are declared with the val (for value) keyword. In the example above the HelloWorld class has a mutable property named name. Note that the property is declared directly in the constructor. Because the property has been declared with var keyword, we can alter its value, as shown in the following example.

var helloWorld = HelloWorld("Kotlin")
helloWorld.name = "Kotlin again"

The value of the property may be changed by assigning it directly to the property. If we would have declared the property using then val keyword, assigning a new value would result in a compiler error. Also please note that there’s no new keyword in Kotlin.

Functions

Kotlin classes have functions that are declared with the fun keyword. A function may have parameters and may have a return type. The return type Unit is what void is in Java. If a function is not a “Single-expression functions” (to be covered in a later article) and returns Unit, then the return type may be omitted.

The greet() function in the example above returns a String value. Note that the returned String contains a template expression that allows you to avoid ugly String concatenation using + operator. A template expression starts with a dollar sign ($). The value after $ represents a named placeholder that is evaluated at runtime. In this example the expression refers to the name property.

Null-Safety

Did you notice the question mark (?) in the System.out?.println statement? Kotlin distinguishes between nullable and non-nullable references. The ?. operator is called a safe call. It’s Kotlin’s way to avoid NullPointerException. If the expression on the left hand side refers to null, the expression on the right hand side is not executed. More on Null-Safety in some of the next articles about Kotlin.

And last but not least, did you notice that we didn’t use any semicolons?

IDE support

Does Kotlin have IDE support? Well, JetBrains is the company behind IDEA which is the best IDE for Java. But also Groovy and Scala support in IDEA is excellent. It’s natural that JetBrains provides IDE support for its own language. I played around with the Kotlin plugin for IDEA. It needs some final polishing but it is already quite usable. Here is a screenshot.

That’s it for now. In this article we cover a few basic Kotlin features. I hope you enjoyed it. Stay tuned.

 

  1. itsadok
  2. Massimo
  3. Justin Malcolm
  4. Igor Drobiazko
  5. Igor Drobiazko
  6. guest
  7. Carl Smotricz
  8. Steve

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress