A Developer’s Look at Kotlin

Named after an island near St. Petersburg, Russia (take a gander at its spectacular sunset, above), Kotlin is a statically typed open-source programming language that runs on the JVM, just like Java. JetBrains released version 1 in February 2016, after five years of development.

Originally coded by a team of Russian developers, Kotlin positions itself as an industrial-strength object-oriented language superior to Java. It uses Java code from the existing Java Class Library, and developers can use it to develop Android apps or generate JavaScript source code. You can run it online in Intelli Idea, the JetBrains IDE.

Here are some additional features that make Kotlin different from Java:

Either of the above works. (Comments are either /* .. */ for blocks or // for single lines, and you can nest block comments.) In the below, note how variable declarations are the Pascal way around; var means mutable (i.e., it can be changed), while val declares them read-only, so you must assign a value there:

Note that primitive types (Int etc) in Kotlin are objects (unlike in Java), so this works:

println(0x0e.toString())

Syntax is straightforward, but make sure that expressions have brackets. Ifs can be used as expressions or statements and blocks. Anything inside { } can have a value. Else is optional inside statements, but mandatory in expressions:

Objects and Classes

The example below shows a simple Dice class with a constructor that passes in the maximum dice value. Two private immutable properties hold the Java.util.Random instance and the maxvalue. Note that the declaration of the Random instance does not use new. A public function Roll() is called for Random; this uses the single line function form with = instead of return.

In the main function, an instance of this dice class is created and a for loop calls Roll() ten times:

The constructor (called a primary constructor) is part of the class header. It can’t contain any code except in an init { } section. It’s also possible to do this without init by initializing the maxvalue property directly; the init can then be removed.

private val maxvalue: Int = maxValue

Delegated Properties

Properties can have getters and setters, though immutable ones only get a getter. Usually properties are either reading from or writing to a backing field; getters and setters can extend this via delegated properties. The syntax of a class declaration in this case looks like this:

class Example {
var p: String by Delegate()
}

There are a couple of delegate types. Let’s start off with ‘Lazy’ for lazy initialization, which calls a lambda expression on the first access; this value is used on subsequent ones. Here’s lazy in use:

This shows that despite accessing sample.lazy twice, only the first access initialized the value. If you comment out the three printlns and run it, you’ll see that there’s no assignment printed, as lazy is never assigned a value. That “by lazy” defers it until reading.

Next up on the delegate types: ‘observable.’ In this example, a TaxRate value has been made ‘observable,’ and defaults to the value passed in: 18.5. But changing it explicitly invokes the handler, which prints out that it has changed. It can be very useful when you need to find out what is changing a property:

Conclusions

I’m a great believer that reduced cognitive load is good for programmers. It’s easier to understand code with simpler, more powerful syntax, and Kotlin certainly delivers on that.

Kotlin has been compared to Scala, though is somewhat simpler and compiles a lot faster. It’s not quite a snapshot of the language, but the Idioms page tells you a lot about features and syntax in one go.

Any developers wanting to know differences between Kotlin and Java at a glance can refer to the Comparison to Java page via Kotlin’s reference materials.

Related Jobs

Salary Predictor

Estimated base salary using job title, location, and skills.

More About Dice Salary Predictions

Where does this estimate come from?

Dice predicts salary ranges based on the job title, location, and skills listed in
individual job descriptions. Our proprietary machine-learning algorithm uses more
than 600,000 data points to make its predictions. To get the most accurate
prediction of the salary you might earn, customize the prediction in your Dice
profile. Actual salary offered by employer may vary.

How was this salary estimate calculated?

Dice's predictive salary model is a proprietary machine-learning algorithm. Unlike
many other salary tools that require a critical mass of reported salaries for a
given combination of job title, location and experience, the Dice model can make
accurate predictions on even uncommon combinations of job factors. The model does
this by recognizing patterns in the more than 600,000 salary data points to infer
how much each factor - job title, location, experience, education, and skills - will
impact the salary.