An Introduction to Kotlin

Introduction

Ever since Apple released the Swift programming language for iOS, lots of Android developers have wanted a similar language for Android development. If you are one of those developers, you are going to love Kotlin, a JVM language that is remarkably similar to Swift.

Kotlin is a statically-typed language, developed by JetBrains, whose syntax is more expressive and concise than that of Java. With features like higher-order functions, lambda expressions, operator overloading, string templates, and more, Kotlin has a lot more to offer than Java. Because Java and Kotlin are highly interoperable, they can be used together in the same project.

If you are a competent programmer in Java, you will be able to learn Kotlin in a very short time. In this tutorial I will be showing you how to use Kotlin’s most frequently used constructs.

Prerequisites

To follow along with me, you will need:

1. Classes

To create a class in Kotlin, you have to use the class keyword. For example, here’s how you create an empty class called Person:

Adding Properties

A class usually has properties and member functions (also called methods). Let’s add two properties to the Person class, name of type String and age of type Int.

As you can see, the syntax for creating variables is slightly different from that of Java. To create a variable in Kotlin, you must use the var keyword. However, if you want your variable to be a read-only/assign-once variable, use the val keyword instead.

For the sake of null safety, Kotlin also makes a distinction between variables that can be null and variables that can never be null. In our previous example, both the variables name and age can never be null. If they are null, the compiler will raise an error.

To create a variable that can hold null, you need to add a ? after the variable’s type. For example:

Now that we have a class, creating an instance of it is easy:

No, Kotlin doesn’t have the new keyword. Once the instance has been created, you can access its properties the same way you would in Java:

Using Constructors

Initializing the individual properties of our instance the way we just did is not a good coding practice. A better way to do this would be by using a constructor. Kotlin’s syntax to create such a constructor is very compact:

In fact, if you have nothing else to add to your class, you don’t need the curly braces. The following code works just fine:

It might already be obvious to you that there’s no way you can add custom code to this constructor. This constructor, which is part of the class’s header, is called the primary constructor.

To add more constructors to your class, known as secondary constructors, you should use the constructor keyword. Secondary constructors should delegate to the primary constructor using the this keyword. Let’s add a secondary constructor to our class that initializes the value of a property named email:

To create an instance using the secondary constructor, you write something like this:

Adding Member Functions

In Kotlin, functions are created using the fun keyword. Let’s add a simple member function named isEligibleToVote that returns a Boolean value:

Note that the return value’s type is indicated at the end of the function header. You can call this method the same way you would in Java:

Creating Extensions

Kotlin lets you extend a class by adding additional functions to it without modifying its original definition. Such functions are known as extensions. The names of these functions should be preceded by the names of the classes they extend.

For example, to add an extension called isTeenager to the Person class, you write the following code outside the class:

This feature is especially useful when you want to extend classes that do not belong to your project’s code base. For example, the following code snippet adds an extension containsSpaces to the String class:

Creating Derived Classes

It’s important to keep the following in mind when creating a derived class:

  • You must use a : instead of Java’s extends keyword.
  • The header of the base class should have the open annotation.
  • If your base class has a constructor that takes parameters, your derived class should initialize those parameters in the header itself.

Let’s create a class named Employee that derives from Person:

Overriding Member Functions

In Kotlin, you have to explicitly state that a member function can be overridden by using the open annotation in the method’s header in the base class. In the derived class, open functions can be overriden using the override annotation.

For example, to override the isEligibleToVote method, you add the following code snippet to the Employee class:

Creating Static Methods

Kotlin does not allow you to create static methods. However, it does allow you to create package level functions that do not belong to any class.

The main method is perhaps the best known static method. If you want to add the main method to a package named com.tutsplus.code.tutorial, then your code would look like this:

2. Functions

You’ve already learned how to create simple functions in the previous examples. The syntax you used to create those functions was very similar to that of Java. Kotlin, however, lets you to do a lot more with functions.

Creating Single Expression Functions

If a function returns the value of a single expression, then you can use the = operator after the function header followed by the expression to define the function.

For example, to add a method to the Person class that returns true if the person is an octogenarian, you would write:

As you can see, this shorthand syntax is more compact and readable.

Higher Order Functions and Lambda Expressions

Higher order functions are functions that can return functions or accept functions as parameters. A lambda expression, on the other hand, is just a function that has no name. Usually, lambda expressions and higher order functions are used together.

Consider the following example that demonstrates the syntax of a lambda expression:

This is a simple lambda expression that takes two parameters, x and y, and returns their sum. As you might have guessed, the parameters of the function are listed before the -> operator and the body of the function starts after the -> operator. This lambda expression can be assigned to a variable and used as follows:

Note that the type of the variable that holds the lambda expression specifies the types of its parameters and its return value.

Creating a higher order function that can accept the above lambda expression as a parameter is just as easy. For example, to create a function that doubles the result of the lambda expression, you would write:

You can call this function as follows:

Alternatively, you can pass the lambda expression directly to the higher order function:

Lambda expressions are often used with arrays. For example, consider the following array of Int values:

If you want to square the value of each item in the array, you can use the map function along with a lambda expression as follows:

3. Ranges

Range expressions are used very frequently in Kotlin. You already used them while creating the isTeenager and isOctogenarian methods.

To create a range, all you need is the .. operator.

To create a range in descending order, use the downTo function instead.

If you don’t want the step to be 1, you can specify a custom value using the step function.

You will see more range expressions later in this tutorial.

4. Conditional Constructs

if

In Kotlin, if is an expression that returns different values depending on whether the condition has been satisfied. The following example illustrates how this works.

when

The when expression is equivalent to Java’s switch. However, it is a lot more versatile. For instance, consider the following example.

As you can see, when can not only take single values but also expressions as its conditions.

5. Looping Constructs

for..in

In Kotlin, you can use the for..in loop to loop through arrays, collections, and anything else that provides an iterator. Its syntax is almost identical to that of Java, except for the use of the in operator instead of Java’s : operator. The following example shows you how to loop through an array of String objects.

With the help of range expressions, you can make this loop behave like a traditional, C-style, for loop.

while and do..while

The syntax of while and do..while loops in Kotlin is identical to the syntax used in Java. For example, the following Kotlin code loops over an array of String objects using a while loop:

6. String Templates

Kotlin lets you embed variables and expressions in strings by wrapping them in a pair of curly braces, prefixed by a $ symbol. For example:

Conclusion

In this tutorial, you learned the basics of the Kotlin programming language. Even though we covered several constructs here, Kotlin has a lot more to offer. In fact, as of June 2015, Kotlin is still available only as a preview. It is likely to see several improvements and new features in the coming months.

If you want to know how to use Kotlin in Android Studio, then take a look at How to Use Kotlin in Your Android Projects . To learn more about the Kotlin language, I recommend visiting the Kotlin documentation.

Tags:

Comments

Related Articles