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
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:
Start Intellij and select “Create new project”
Give the project a name and click on the next button.
In the left pane select “Kotlin (Java)”
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
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)
Now select the package with the right mouse button and create a new kotlin file. Name it helloworld.kt ( how original 🙂 )
Now your project structure must look like:
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:
To run the code click with the right mouse button on the kotlin file and select “run kotlintest”
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 :
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.