Teknologi

I Bouvet er vi flere hundre teknologer som brenner for å programmere og utforme gode, digitale løsninger. I denne bloggen utforsker vi teknologien og deler det vi finner med dere.

Kotlin – an Introduction

At Google I/O 2017, Google announced that Kotlin would become a first-class language in Android. This means that Kotlin will be supported on the same level as Java and C++ on Android. In this post I’ll be presenting what Kotlin is, along with some of the features that
excite me about it.

What is Kotlin

Kotlin is a static typed JVM language. Unlike Scala, it’s not an academic language, but rather a language developed by the industry.

Kotlin was developed by JetBrains, the makers of IntelliJ. JetBrains wanted a new language that would address some the issues they had with Java, but at the same time needed to support their legacy Java code. Kotlin was their answer to these two problems.

Features of Kotlin

  • 100% interop with Java
  • Conciseness

Interop, interop, interop

One of the key requirements of Kotlin was that it had to have 100% interop with Java. Since JetBrains had a huge code base in Java, they wanted a language that would use the existing code base without having to throw features out when converting to Kotlin.

What does this mean? It means that a project can have both Kotlin and Java living side-by-side without problems. Each side can make call to the other side.

Consider the following code snippets.  The first is in Java:

And here is the equivalent code in Kotlin:

Since this is a data class (more on that in a later post), getters/setters, hashCode, equals, and toString() are generated automatically.

To access the Kotlin class from Java one could use the following code:

And to access the Java class from Kotlin one could use the following:

There is no requirement to «convert» between data structures when calling code across the Java / Kotlin boundary.

By default Kotlin will compile down to Java 6 bytecode. This means that Kotlin is supported on the JVM from Java 6 to the current version. In Kotlin 1.1 support was added for compiling to Java 8 bytecode.

Because interop was a design goal, Kotlin does not come with its own collection classes, unlike other languages such as Scala. Instead, JetBrains opted to extend the standard Java collection by using extensions (more on that in a later post).

Now, since functions are first-class in Kotlin, how does Java code call them? JetBrains has dealt with this by making the file name the class name in Java.

bar() will then become a static method in a java class called bouvet.ExampleKt. You can specify what the java class will be called by using @file:JvmName(), like this:

Here bar() will be in a class called bouvet.ExampleUtil.

Conciseness

Have you have ever asked yourself why you are forced to add so much boiler-plate code in Java, when the compiler should be perfectly capable to figure out what you mean? If you have then Kotlin is for you.

Back to our Kotlin example:

The semicolon is optional, since the compiler can figure out it’s the end of the statement.

Neither money nor str need a type because the compiler can figure it out. If you want to give the variable another type, you can specify it.

Kotlin does string manipulation. And if your function is one line, you can just assign the operation to the function.

Kotlin lets you deconstruct a type. Say you want to assign value and currency to variables from the Money class. This is how you would do it in Kotlin:

A lot of the boiler-plate code that you write or are generated by theIDE for you in Java, you can just skip in Kotlin.

Part of the reason Kotlin is more expressive and more concise (while still being readable) is because the Kotlin compiler is «smarter» than the Java compiler.

As an example let us look at smart casting. In Java, you need to write code like this:

While in Kotlin:

Since you have already checked whether bar is an instance of FooBar, within that if-block, bar will behave like FooBar.

Tools

Since Kotlin was originally created by JetBrains, you can expect that IntelliJ has great support for it. And since Android Studio is based on IntelliJ, it will also have great Kotlin support.

JetBrains also has created plugins for Eclipse (although this is still in beta and probably
doesn’t have as good support as IntelliJ), and you can also build Kotlin from the commandline.

In future posts, we’ll look at the nullsafety, type system, lambda, classes, and extension functions. And be on the lookout for a Kotlin/Android talk in a future Bouvet event.

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *

Eress Forum 2017

Eress and Erex Eress is an organisation created to provide a simple, efficient, reliable, accurate and flexible standard energy settlement..

Devoxx UK 2017

Introduksjon Devoxx UK er en todagers konferanse som holdes i London. Det er en mellomstor konferanse med over 1200 deltakere..

Bouvet Battle Royale – Robot Wars

Introduction Robot Wars, a robot-sumo competition, was held at Bouvet early April for students attending technology courses at the University in..

Magic Mirror – version 1

Introduction A while back I discovered the exciting world of “magic mirrors”. I don’t remember how or where it caught my attention, but..

DevOpsDays Oslo 2016

5.-6. september hadde eg gleden av å delta på den første norske DevOpsDays i Oslo. Her er en oppsummering av høydepunktene..

Bouvet at JavaZone 2016

This year JavaZone celebrated it’s 15th year with with 3000 attendees and over 170 sessions. As one of Norway’s premier Java..

IT years are like dogs years

One of the characteristics of the IT industry is that time works differently for us. This is challenging and fun,..

The Future of SharePoint

Den 4. mai holdt Microsoft en virtuell event om fremtiden til SharePoint, jeg fikk heldigvis anledning til å delta de..

SharePoint 2016 er på vei!

Som lovet var Microsoft ferdig med utviklingen av SharePoint Server 2016 (RTM – release to manufacturing) rett før påske, og..

Teknologi

I Bouvet er vi flere hundre teknologer som brenner for å programmere og utforme gode, digitale løsninger. I denne bloggen utforsker vi teknologien og deler det vi finner med dere.