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 or SWIFT, you will be able to learn Kotlin in a very short time. Additionally, learning Kotlin can be beneficial because you will find it quite similar to not only Swift, but also C#, and JS. In this tutorial, we will go through how to use Kotlin’s most frequently used constructs.

To follow along with me, you will need:

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:

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.

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

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:

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

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:

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:

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:

In Kotlin, you have to explicitly state that a member function can be overridden by using the openannotation 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:

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.petaminds.code.tutorial, then your code would look like this:

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.

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 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:

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.

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.

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

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.