Kotlin: JetBrains’ JVM goodness


  This entry is part 1 of 3 in the series Learn me some Kotlin


This is the debut of a new series called Learn me some Kotlin. The series is aimed at beginner to intermediate Java developers like myself (I’m writing these articles as I learn the language) although it will be easily understandable by anyone with a prior exposure to an object-oriented language.

Kotlin is a programming language targeting the Java platform. It hit version 1.0 in February this year (1.0.3 is the latest stable version) but has been in heavy use at JetBrains (the makers of the language) for several years now (long before they open-sourced it in 2011).

Why Kotlin

Why is Kotlin a worthy contender in an already crowded JVM space? Well, Kotlin positions itself as a concise, safe and pragmatic language for the JVM with a focus on interoperability with Java.

Answering “Why Kotlin” can be closely associated with some of the design goals that JetBrains chose while developing Kotlin and that’s where we’ll start looking.

TL;DR We’re not looking at any code in this article. In case you want to jump right in, skip over to the installation and setup and then head over to the next article (although it’s highly recommended if you at least skim through, to get a context about the discussions ahead).
  • Interoperable with Java
    Kotlin compiles to JVM bytecode (or Javascript) and could easily be used anywhere Java is being used today. But it takes the interoperability to the next level by allowing developers to:

    • call Java code from Kotlin (obviously)
    • call Kotlin code from Java (no magic, all the heavy lifting is done by Kotlin)
    • mix the two in a single codebase (as a result of the effortless two way invocation)

    We have some obvious advantages:

    • We can use all the existing Java libraries and frameworks in the Kotlin code- invoke methods, extend classes and implement methods, apply annotations, so on and so forth. For instance, Kotlin does not have its own collections library but builds upon the Java collections framework.
    • There is virtually no run-time overhead – the standard library is compact and even the functional constructs are optimized at compile-time with in-lining.
    • It lowers the barrier of adoption of Kotlin. It’s bound to entice people working with Java as a more expressive and less verbose alternative (or even Scala, Groovy developers, but let’s not pull at that thread yet ;)).
  • Pragmatic
    Kotlin is a pragmatic language coming from the industry (as opposed to research languages developed in academia). It is a practical language designed to solve the problems faced by the real-world developers (esp. those working on large-scale projects).
    It has adopted ideas and traits that have proven to be successful over the years in existing languages. Kotlin does not enforce any particular programming paradigm like predominantly object-oriented or functional, which coupled with its lean and readable syntax makes learning and getting started a breeze.
    And coming from JetBrains, we can be rest assured that the tooling aspect is taken care of- the Kotlin plugin for IntelliJ IDEA is fully featured that can handle an arbitrary mix of Java and Kotlin code, debugging/profiling and offers converting Java code to equivalent Kotlin code.
  • Safe
    Kotlin aims to achieve a higher level of safety than Java. Right off the bat, it benefits from being a statically typed language on the JVM, in terms of the safety guarantees. Exploring further, there are a couple of interesting ideas that Kotlin has introduced: Null Safety and the is operator for class casts.
    Null Safety
    The Null safety provided by the language’s type system is a great step towards eliminating NullPointerException from the code. Variables and properties cannot be null in Kotlin unless explicitly allowed thus neatly avoiding invoking methods on null values inadvertently.
    (It’s an interesting topic which we’ll get into deeper in a later article but this gives a good coverage)

    Class casts
    It’s commonplace in Java for developers to skip type-checking before casting an object since the cast has to be explicitly done even after the check. This may in some unfortunate scenarios, lead to a ClassCastException. Kotlin makes it easier by combining the check and the cast in a single operation (with the is operator).

  • Concise and Expressive
    JetBrains defines a concise language as one that can clearly express the intent of the code with its syntax and does not obscure it with boilerplate required to accomplish the intent. Kotlin balances terseness (by removing clutter, for instance implicit getters, setters and constructors for Kotlin classes) and expressiveness (by ensuring we need to just write code that is meaningful) well.


Primary Features

A general overview of the primary traits of language.

  • Statically typed
    Kotlin is a statically typed language which not only provides compile-time validation but also adds to succinctness by determining the type from the context which is known as type inference. It also benefits from static typing in terms of performance, maintainability and tooling support.
  • Mixed paradigm
    As we have discussed above, Kotlin does not enforce any particular programming philosophy. It’s as object-oriented as Java but provides enough syntactic and library support for seamlessly adopting a functional style. It lifts functions to a first-class citizen status, supports higher-order functions (functional types) and immutable value objects (data classes) and provides a rich set of APIs in the standard library.


Setting up the Development environment

The Kotlin compiler kotlinc compiles the source code (in files with an extension .kt) to generate .class files. The compiled code needs a Kotlin runtime library to execute and must be distributed with the application.
While Kotlin is fully compatible with build systems like Maven or Gradle, let’s just begin by setting up the command-line compiler (which provides an interactive shell as well) and IntelliJ IDEA.

  • Command-line Compiler
    Although we can go for a manual install using the binary distribution, the preferred way to install Kotlin on a Unix based OS (Ubuntu in my case) is by using SDKMAN! like so (follow any instructions that come along)-

    Note: You can consult this guide for a manual install on Windows or via Homebrew on OS X or in case of any other assistance.

    Next, install Kotlin-

    Now you can invoke the interactive shell by firing-up the Kotlin compiler-

    Now let’s take a step ahead and execute a Kotlin file. Here’s the source code-

    That’s all. In Kotlin, we can have top level functions in a file and the one above just has the main function. Let’s compile this into a distributable jar (along with the Kotlin runtime) and execute it.

  • Getting started with IntelliJ IDEA
    Kotlin plugin for IntelliJ IDEA is fairly easy and intuitive to get started with (esp. for people familiar with IDEA for Java). I’ll skip the steps here and direct you to the official tutorial for getting started with IntelliJ IDEA.

It’s a wrap! We’ve just looked at what Kotlin is and what it has to offer. The early impressions of the language have got me fairly intrigued and I wish to delve much deeper into the more powerful constructs and features.
Next up, we’ll get a taste of Kotlin by looking at some code.



Some resources:
. Home page: Kotlin Programming Language
. Reference: Kotlin Reference
. Community: Kotlin Community

Series NavigationKotlin Basics I – If you could show me some code, that’d be great >>

Leave a Reply