Hello, Kotlin!
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.

At first glance looks like Groovy except for properties being held in class arguments.
And what’s the big difference to Scala? Looks quite the same to me so far.
You actually did use a semicolon – line 6.
While I’m nitpicking – the title just above the field I’m entering this into says “Leave a reaply”.
Could you kindly explain how Kotlin is different from Scala? It seems like Scala, but ever so slightly different. What would I choose Kotlin over Scala, and how is it going to do better than scala, for example in areas such as binary compatibility?
I see a semicolon
–> return “Hello, $name”;
(ps this box should be titled “leave a reply”, not “reaply”)
I notice that you did use a semi-colon:
> return “Hello, $name”;
Was that required or is it just an old habit sneaking in?
Thanks for the hint regarding the semicolon in line 6. It’s indeed not needed. Just a habit from Java. I fixed the example.
@Christian & @Aaron: I think comparing these two languages is not really fair right now as Kotlin is not yet done. From what I’ve seen so far, Kotlin code is more easy to read.
Here is what Kotlin documentation says:
If you are happy with Scala, you probably don’t need Kotlin.
More details are available here: http://confluence.jetbrains.net/display/Kotlin/Comparison+to+Scala
You can reduce the example further to:
package hello
class HelloWorld(val name: String) {
fun greet() = "Hello, $name"
}
fun main(args: Array) = System.out?.println(HelloWorld("Kotlin").greet())
It is valid Kotlin at the time of writing, you can try it in version 0.1.296 of the Kotlin Web Demo.
Where can I download IDEA plugin?
@Christian & @Aaron:
I’ve read a couple of Scala books and written a few smallish programs in it; also, I’ve read my way through the Kotlin documentation. So I’m marginally qualified to answer your question.
The answer is that Kotlin is less than Scala. Some people enjoy having all the features and shiny buttons they can get a hold of, but as someone looking to explore the language as a hobbyist sideline rather than fulltime professional, I found myself terribly frustrated by the complexity of Scala. The various flavors of syntactic sugar, the fetish-like emphasis on types, some quirkiness of the library. Others have written more eloquently about this, but they claim (and this has been my experience too) that it’s not possible to code Scala even at the “programming grunt” level without immersing yourself in large parts of a complex language.
Compare with Lua: Starting from zero, I was able to program productively after reading the book, i.e. Lua has something like a 2 hour learning curve. Scala is completely unlike that.
I think Kotlin is to Scala as Java (say, Java 2) is to C++: less magic, less complexity, a smaller and (IMO) more sensible subset of features. Easier to learn and likely more fun.
@Carl: Is that wishful thinking or backed by any facts?
From my experience with both Scala and Kotlin it is more or less the first option. They keep promising things and fail to deliver.
@guest: see here: https://twitter.com/#!/alextkachman/status/155951220698259456
I don’t see the primary value proposition of that new language? Make Java simple like Scala? Make a Javascript-like language better than Dart? Make a server-side language more concise as Go? What’s the whole point?
IMO it’s DOA product.