1. Introduction

Kotlin is a JVM programming language, meaning it compiles to Java bit code, it runs on the Java Virtual Machine and it’s fully co-operational with other JVM languages like Java and Scala.

One of Kotlin features is the support for immutability. There are two kind of values – values (val) and variables (var). If you declare them as variable then the value can change if you declare as value then their value is fixed at initialization time so they are immutable. Immutability is very important for us because it’s one of the keystones for example in functional programming.

Another thing about Kotlin is that it fights back if the programmer tries using nulls. It either doesn’t let us (as much as possible) use nulls or whenever we are using nulls, it forces us to understand that we are using nulls and that we need to write code to coop with those nulls. But don’t worry, this doesn’t mean you will have to write 10’s of extra code lines, all can happen in a single line using the Elvis operator (?:).

Kotlin offers support for functional constructs. Things like immutability, which we already mentioned but also things like higher-order functions. We can pass functions into functions and we can return functions from a function. This is very well integrated and very easy to use, the syntax is similar to lambdas in Java 8.

Among all things, Kotlin is also Object oriented. You could create classes and from those classes create objects. However, Kotlin doesn’t have a static keyword. As we know Java and the JVM have support for static classes and members so Kotlin has to facilitate around this. We will find out how this can be done exactly in one of the next chapters here about Kotlin.

Compared to Java, Kotlin feels less ceremonial. There is little to none need to write boilerplate code. For example, when you need to declare a Java bean you just create a variable and that’s it, no getter and setter are required. Or the fact that you don’t need to specify the type of value, Kotlin has great deal of type inference and it will be able to figure out the type upon initialization. Furthermore, I will again mention that Kotlin has a first class support for functions and this ends up in having a much cleaner code when we pass functions around.

To highlight all these features, we could say that Kotlin is a very good language for writing domain specific languages (DSLs). For example, we will see that when it comes to writing test languages Kotlin offers great support in doing that.

Now, let’s take a look at simple Kotlin code:


fun max(a:Int, b:Int) : Int {

    return if(a>b) a else b

}

This is how we define functions. Key points to notice are:

  • fun – keyword for declaring a function
  • types are declared after the variable name and the return type at the end of the function declaration
  • if  – is an expression, not a statement, it will actually return something

Here is an example of immutability and how we are declaring values:

//immutable value, can be initialized only once
val name:String = "Ivo"

//mutable value, can change during the lifetime of the application
//notice we didn't specify the type here and this is totally fine
var familyName = "Mladenov"

Another great thing is the ability to work with string templates:

val name:String = "Ivo"
print("My name is $name")

In the print the $name will be replaced by the value of name.

Finally, let’s take a look at a class example:

class Person (val name:String)

Here, we define a class named Person. The construct that you see (val name: String) is what’s called a primary constructor for the class. We don’t need a separate method definition that says Person and takes a string value as a parameter called ‘name’. We can declare that all in one line. This will also declare a property for us called ‘name‘ to hold that value.

We can also declare properties in a class like this:

class Person {
val name: String
val age: Int
}

Notice there is no need for getters and setters. Which equals less code, equals less bugs. 🙂

 

Object Oriented Programming >>