Introduction to Kotlin

1. Overview

This lesson will introduce you to the world of Kotlin, a new programming language by JetBrains which is now officially, a first-class citizen for Android.

This means that you are free to develop Android apps in Kotlin as well. Apart from Android, you can always use Kotlin in Server-side applications as well, like with Spring Boot development. Let’s look at this wonderful language to start our journey.

Note that this lesson is not meant as an introduction to programming, so, familiarity with some programming constructs is expected.

2. Maven Dependencies

To demonstrate the Android examples in this lesson, we will make a Maven based project and start adding relevant examples.

For Kotlin support, we need to add a single dependency in pom.xml file:

As expected, a Kotlin program starts with a package statement which informs the compiler about the package this file should be scoped to.

Next, we create a function and call it main(…). Existing Java Developers will find this extremely similar to Java because it actually is. main(…) is the first function which is called when a Kotlin project is run.

Go ahead and run this program and we’ll see the magical message for “Hello World!”.

3.2. Creating Variables

In Kotlin, Variables can be defined in many ways.

To create final variables, also termed as constants, use the val keyword:

val myAge = 21
val myLanguage = "Kotlin"

Now, clearly, the data-type of the variables was not mentioned here. This was because Kotlin can interpret the data type of a variable from the initial value it is provided. So, myAge final variable is of type int and myLanguage is of type String.

To create variables with no initial value, we can use the var keyword:

var myName: String? = null
var loveKotlin: Boolean? = null

Notice closely how we made use of the var keyword along with the Datatype and a ‘?’. In Kotlin, whenever we need to define a variable with no initial value, we need to use the ‘?’ operator with the Datatype for Null-check. We will read this in detail in a later section.

3.3. String Operations

Kotlin provides an excellent way of performing String related operations. Let’s see some examples to support the claim.

Printing a String variable is easy:

fun main(args: Array<String>) {
println(myLanguage)
}

Formatting String is even easier:

fun main(args: Array<String>) {
println("I love $myLanguage")
}

The $ symbol formats the String and the expression is evaluated before the actual print call. This will print the message “I love Kotlin”.

In above code snippet, studentName will be assigned a value based on the value of studentId. Note that we just compared Strings using ‘==’ operator. We can do this in Kotlin. Though it is obvious, using if statement normally is possible as well:

3.6. Classes

That’s easy. But wait, where are those getters and setters? With Kotlin, we don’t need them as those are managed by Kotlin internally itself.

If a model is made just for the sake of holding data, like above class Student, it should be made as a data class. Data classes have just one purpose, to hold data. Let’s transform above class to a data class: