Kotlin introduction

Kotlin

This is a blog series on Kotlin.

  • Kotlin Introduction

As a Java developer I’m interested in the Kotlin project from Jetbrains. I like to learn new technologies and will post my experience on this blog.

Kotlin is a new programming language from Jetbrains. A word of warning: the language is not finished yet, so nothing in this blog is final. The Kotlin project is open source and can be found on github

Kotlin is a JVM language like scala and groovy. The code compiles to JVM byte code and to javascript. Most development effort went to the JVM part, but lately there is some progresss for the javascript part. For those developers that use the Live Edit plug-in from Jetbrains, this is a product that uses this capability.

At this time of writing there is only a plug-in available for Intellij, but an eclipse plug-in is under development.

What are the goals of Kotlin

  • Full Java Interoperability
  • At least as fast as Java
  • Safer than Java
  • More concise than Java

Introduction – How to create a Kotlin project

I’m using the Intellij Ultimate Edition version 13 EAP but Kotlin can also be used with the free Community edition.

I’m going to show you step by step how to create a hello world Kotlin application.

The first thing you need to do is install the Kotlin plug-in. Start intellij and on the welcome screen select Configure -> plug-ins. Enter Kotlin in the search field and select install. You have to restart the IDE after the download.

Let’s start by creating a simple Java project:

Create a project
Create a project

Start Intellij and select “Create new project”

Create project name
Create project name

Give the project a name and click on the next button.

Select Kotlin
Select Kotlin

In the left pane select “Kotlin (Java)”

Right pane with Kotlin selected
Right pane with Kotlin selected

Kotlin Dialog
Kotlin Dialog

Click on the create button and a new dialog opens. Here you can choose to copy the Kotlin JAR library into your project or use it from the Kotlin plug-in. The first option is interesting if you want to put the library under version control. I choose “Use library from plug-in”. Click on the finish button.

Intellij opens with following project structure

Folder structure start of a new project
Folder structure start of a new project

Create a new package in the src folder. I named mine “kotlin test” (For those who are not familiar with intellij: Click with the right mouse button on the src folder -> new -> package)

Create new package
Create new package

Now select the package with the right mouse button and create a new kotlin file. Name it helloworld.kt ( how original 🙂 )

Create new kotlin file
Create new kotlin file

Now your project structure must look like:

Project structure after creating a new kotlin file
Project structure after creating a new kotlin file

Now I’m going to show you the Kotlin equivalent of Java hello world:

In the helloworld.kt file enter main and hit the tab key. Intellij will insert the main method automatically. Now enter following code inside the main block:

Kotlin hello world code
Kotlin hello world code

To run the code click with the right mouse button on the kotlin file and select “run kotlintest”

Running the application for the first time
Running the application for the first time

If there are no errors Intellij does 2 things for you:

  • It runs your application and puts the output in the console
  • It creates a run configuration. So if you want to run the application again you just have to click on the run button in the tool bar at the top.

The results must look like :

Run configuration
Run configuration

Console output
Console output

If you read the hello world code there are a few things that are worth mentioning. To print something to the standard output only a short println() is necessary. There are also no semicolons at the end of a line. You can put them in but you don’t have to, which makes the code nice and concise.

This was my first blog post about Kotlin. The next time I’m going to explore how Kotlin handles null pointer exceptions.