dark mode light mode Search Menu

Kotlin: A New(-ish) Kid on the Block

In this article, we’re going to introduce a programming language that’s getting a lot of buzz: Kotlin. Kotlin is about six years old now, young by programming language standards, but it has the distinction of being one of the only languages officially supported for Android development! It’s even being used by companies like Pinterest and Coursera for their Android apps, which is pretty impressive for such a relatively young language!

Kotlin was developed by JetBrains, the company behind IntelliJ which is one of the big development tools for Java, to be a language that can function as a drop-in replacement for Java. Kotlin is meant to address some of the complaints people have about Java, many of which are related to how infamously verbose it is.

Kotlin is based on the same framework as Java, though, which is the Java Virtual Machine (JVM) and all the library code that comes with it. A virtual machine is a lot like an emulator for an old video game, except that instead of having a program that pretends to be a Super Nintendo running on your computer it’s a program that pretends to be an imaginary computer whose only job is to run something called JVM bytecode.

The way Java solved the problem was to shift the burden of portability from the working programmer to the Java implementation itself. The programmer is completely isolated from the difference between operating systems and hardware and, instead, writes code for the virtual machine. It was the job of Sun Microsystems, and now Oracle, to make programs that could run the virtual machine on every platform.

This portability and history is why there’s a number of languages that are compiled to JVM bytecode, such as Scala, Clojure, and now Kotlin.
So what does Kotlin look like?

Well here’s an example of a Kotlin program that implements a bubble sort where you can provide the function used for comparison as an argument.

fun <T> bubbleSort (l : MutableList<T>, c : (T,T) -> Boolean) : MutableList<T> {
   val length = l.size
   var isSorted = false
   while (! isSorted) {
      isSorted = true
      for(i in 0..length-2) {
	    var tmp = l[i+1]
	    l[i+1] = l[i]
	    l[i] = tmp
	    isSorted = false
   return l
fun main(args : Array<String>){
   var testList = mutableListOf<Int>(4,2,3,1)
   // Here we're going to sort the list from high to low
   // which means that it should flip the two elements when the first is LESS than the second
   bubbleSort(testList,{a , b -> a < b})

and for comparison here’s the bubble sort code in Python:

  def bubble(lst,c):
      n = len(lst) - 1
      isSorted = False
      while not isSorted:
	  isSorted = True
          for i in range(0,n):
              if c(lst[i],lst[i+1]):
                  tmp = lst[i+1]
                  lst[i+1] = lst[i]
                  lst[i] = tmp
                  isSorted = False
      return lst

Now let’s talk about Kotlin and some of the more unusual bits of syntax.

First off, Kotlin is a typed language. That means that every variable, every piece of data, and every function argument has types that are going to be checked before the program is allowed to run! This is as opposed to languages like Python, Ruby, or JavaScript that are untyped languages. This is one of the big differences about how Kotlin and Python look at first glance: we’re having to name all these types.

As an aside, sometimes people will say “statically typed” while I say “typed”, or “dynamically typed” while I say “untyped”. I think typed and untyped get at the heart of it better: are there types that are checked to determine whether a program is “error free” and should be allowed to run?

So when we’re declaring our function that performs the bubble sort we can see the first line looks like:

fun <T> bubbleSort (l : MutableList<T>, c : (T,T) -> Boolean) : MutableList<T> {

This might look a little busy and intimidating, so let’s break it down: we want to sort lists. In Kotlin, lists that you can modify are mutable lists which have a type of MutableList. Our sorting function has two arguments: the list to sort and a function that define what “greater than” means for sorting. If you wanted to, say, sort a list of numbers from low to high, then you’d want to compare them by the normal > operator.

If you’re not familiar with Java, you might not know what this business is. So in Python, for example, you can just write your bubble sort once and it can work on a list that has anything in it. That’s because Python is untyped! In Kotlin, though, you need to use a generic parameter to name the type that the list is going to hold. It doesn’t have to be . It could be for all Kotlin cares. It just needs to be a name!

Another thing worth mentioning is that there’s two different ways to create variables in Kotlin: var and val. var is for things that you want to change over time, like our flag for if the list is sorted! val is for things that shouldn’t change, like our variable that stores the length of the list.

The last unusual bit of syntax is {a , b -> a < b}. Now if you know Ruby you might think this looks a little bit like a block and you’d be absolutely right! You can declare small functions like you might with Python’s lambda keyword or Ruby’s block syntax this way.

If all of this makes Kotlin look interesting or you’re interested in something other than Java for Android programming, then you should go ahead and check out Kotlin. There’s tutorials, essays, and sample code linked at the end of this article!

Learn More

Explanations from the Kotlin team


A description of different languages that compile to the JVM


The Kotlin Koans, an online tutorial


The IntelliJ IDE


Essay about Kotlin from an engineer at Pinterest


Kotlin example code