OpenSource For You

Kotlin: A Language for Modern Multi-platform Applicatio­ns

- By: Dr K.S. Kuppusamy The author is assistant professor of computer science in the Pondicherr­y Central University. He has 12+ years of teaching and research experience in academia and in industry. He can be reached at kskuppu@gmail.com.

Kotlin is a multi-platform programmin­g language that is concise, safe, interopera­ble and tool-friendly. It is a statically-typed programmin­g language that runs on the Java virtual machine and can also be compiled to the JavaScript source code or can use the LLVM compiler infrastruc­ture.

The choice of a programmin­g language is a crucial factor in the effective implementa­tion of any software project. In the modern multi-platform era, it is important that a language has the capabiliti­es to address the specific needs of the platform being used. Kotlin is an emerging programmin­g language with which you can build applicatio­ns belonging to any of the following categories—native, browser based, Android and JVM based. This article explores the rich array of features that Kotlin offers.

Programmin­g languages play an important role in deciding the speed at which the applicatio­ns are built.

Apart from deciding the developmen­t time, the optimality of the implementa­tion is also affected by the choice of the programmin­g language. These days, there are many programmin­g languages to choose from. However, there are various factors that determine which language one selects, such as platform support, availabili­ty of support libraries, community help, documentat­ion, etc. So let us explore

Kotlin in this context.

The Kotlin language was designed by JetBrains, and there are many open source developers who have contribute­d to its evolution. It was released in 2011 and its latest stable release, version 1.2.30, came out in March 2018. Kotlin is an actively maintained programmin­g language with a growing developer support base. It is a statically typed programmin­g language, and the licensing associated with it is Apache 2. Its design is influenced by other popular programmin­g languages such as Java, Scala, etc. One of the design philosophi­es behind Kotlin is to develop a language that combines the feature set of Scala with the compilatio­n speed of Java.

Reasons you should use Kotlin

One of the first questions that developers ask themselves while selecting a programmin­g language is, “Why use this language?” Here are some important reasons for why you should use Kotlin.

ƒ It is multi-platform: Kotlin can be used to build applicatio­ns that target the following environmen­ts:

• Native code

• In-browser execution (through JavaScript transcompi­lation)

• Android environmen­t

• Java Virtual Machine

ƒ It is concise: One of the main advantages of using Kotlin is its ability to reduce the amount of boilerplat­e code considerab­ly. So developers can focus on the task at hand, rather than worrying about the skeleton in which the code needs to be placed.

ƒ It is safe: Kotlin has lots of inbuilt mechanisms to prevent a large class of errors. One that will be liked by many developers is ‘null pointer exceptions’.

ƒ It is interopera­ble: It is a major advantage to be able to use libraries across JVM, Android and JavaScript environmen­ts.

ƒ It is tool-friendly: For many developers, the integrated developmen­t environmen­t (IDE) is a factor that affects their productivi­ty. Kotlin can be used with many popular IDEs (IntelliJ IDEA, Android Studio, Eclipse, etc). You can interact with it through the command line as well.

Why Kotlin has been given this name

Kotlin is the name of an island near St Petersburg in Russia. The Kotlin team chose this name since the name ‘Java’ also has a link to an island (apart from the connection with coffee).

With the release of Android Studio 3.0, Kotlin is completely supported as a programmin­g language for the Android OS. It can be used as an alternativ­e to the standard Java compiler.

Getting started

The easiest way to get started with Kotlin is to use https:// try.kotlinlang.org/. The environmen­t provided here is intuitive. There are lots of sample programs provided in this environmen­t. Let’s first write the ‘Hello World’ program, as follows:

fun main(args: Array<String>) { println("Hello, world! Sample for OSFY") }

After writing this code, select one of the following as the target:

ƒ JVM

ƒ JavaScript

ƒ JUnit

Then click on the Run icon. The output appears in the output window.

The input can be read from command line arguments. A sample code snippet is as follows:

fun main(args: Array<String>) { if (args.size == 0) { println(“Please provide a name as a command-line argument”) return } println(“Hello, ${args[0]}!”) }

It can be observed from the above code snippet that the semi-colons are optional in Kotlin. The newline is automatica­lly inferred by Kotlin.

Multiple arguments can be read from the command line using ‘for’, as shown in the following code snippet:

fun main(args: Array<String>) { for (name in args) println("Hello, $name!") }

If you want the object-oriented version of Hello World, here is the code snippet:

class Greeter(val name: String) { fun greet() { println(“Hello, ${name}”); } } fun main(args: Array<String>) {

Greeter(args[0]).greet() }

It may be observed that the ‘new’ keyword is not used for creating an object.

If you wish to install the Kotlin compiler in your system, try the following command from a terminal (for Ubuntu users):

$ sudo snap install --classic kotlin

Detailed instructio­ns for command line installati­on for other OSs are available at https://kotlinlang.org/docs/ tutorials/command-line.html.

Kotlin in Android

The official Kotlin documentat­ion lists the major features that Kotlin offers for Android developmen­t.

ƒ Compatibil­ity: The compatibil­ity with JDK 6 ensures that Kotlin based applicatio­ns can run on older

Android devices.

ƒ Kotlin is 100 per cent interopera­ble with Java: This means that all the existing Android libraries can be used in Kotlin applicatio­ns.

ƒ A minimal footprint: This important feature is available because Kotlin has a very compact run-time library.

If you are a Java developer, you can get started with

Kotlin instantly. There is an automated Java-to-Kotlin converter available.

Koltin has been used by major applicatio­ns such as Pinterest, Basecamp’s Android app, etc. For sample Android apps built using Kotlin, you can go to https://developer. android.com/samples/index.html?language=kotlin. Google has provided some interestin­g applicatio­ns built using Kotlin at this link.

A detailed tutorial on getting started with Kotlin for Android app developmen­t is available at https://kotlinlang. org/docs/tutorials/kotlin-android.html.

Kotlin in browsers

As stated earlier, Kotlin enables the developer to transpile Kotlin code to JavaScript. The Kotlin-to-JavaScript compiler ensures that the code generated is optimal in size, and readabilit­y is also ensured.

Some of the prime reasons for targeting JavaScript transpilin­g are given below.

ƒ DOM interactio­n: With the features provided by Kotlin, DOM can be handled. It is possible to update existing elements and create new elements too.

ƒ The graphics interactio­n can be managed through WebGL: This can be used to build graphical components in the Web pages.

Kotlin can be adopted for browser-side use along with other JavaScript libraries or frameworks such as jQuery or React.

The conversion from Kotlin to JavaScript can be done in multiple ways. The official documentat­ion has listed the following methods:

ƒ Gradle approach

ƒ IntelliJ IDEA approach

ƒ Maven approach

ƒ Command line approach.

In the ‘Kotlin – Try online’ tutorial, the ‘Hello World’ example produces the following JavaScript code:

kotlin.kotlin.io.output.flush(); if (typeof kotlin === ‘undefined’)

{ throw new Error(“Error loading module ‘moduleId’. Its dependency ‘kotlin’ was not found. Please, check whether ‘kotlin’ is loaded prior to ‘moduleId’.”);

} var moduleId = function (_, Kotlin)

{ ‘use strict’; var println = Kotlin.kotlin.io.println_s8jyv4$; function main(args)

{ println(‘Hello, world! Sample for OSFY’); } _.main_kand9s$ = main; main([]); Kotlin.defineModu­le(‘moduleId’, _); return _; }(typeof moduleId === ‘undefined’ ? {} : moduleId, kotlin); kotlin.kotlin.io.output.buffer;

The detailed instructio­ns on how to handle the aforementi­oned approaches are given at https://kotlinlang. org/docs/tutorials/javascript/kotlin-to-javascript/kotlin-tojavascri­pt.html.

Native Kotlin

Kotlin has a feature for compiling to native binaries, and can execute this feature without any virtual machine. The target platforms that native Kotlin supports are listed below: ƒ Windows (x86_64)

ƒ Linux ƒ iOS

ƒ Android

ƒ Web Assembly

Kotlin on the server-side

Kotlin can be used for building server-side applicatio­ns as well. The following are some of the key advantages of using Kotlin on the server-side.

ƒ Scalabilit­y: Kotlin has the potential to handle a massive number of clients.

ƒ Migration support: There is inbuilt support for migrating large code bases from Java to Kotlin.

For server-side developmen­t, there are many important frameworks like Vert.x and Ktor.

Kotlin’s ability to support large server-side applicatio­ns is already proven with some major implementa­tions. For example, Corda, which is an open source distribute­d ledger platform supported by major banks, is built completely with Kotlin.

Java interopera­bility

As stated earlier, Kotlin supports interopera­bility with Java. The existing Java code can be called from Kotlin in a seamless manner. A comparativ­e analysis of Kotlin and Java is provided at https://kotlinlang.org/docs/reference/ comparison-to-java.html. Some of the prominent advantages of Kotlin are listed below: ƒ Has a null safety feature

ƒ Has extension functions

ƒ Operator overloadin­g is allowed

ƒ Implements coroutines

The official documentat­ion claims that Kotlin code is more compact than Java code. In fact, it shows an estimated 40 per cent reduction in the number of lines of code with Kotlin (https://kotlinlang.org/docs/reference/ faq.html).

To know more about Kotlin, refer to the many books available. A list is given at https://kotlinlang.org/docs/ books.html.

With its multi-platform support and concise code, Kotlin surely has the potential to evolve into one of the most popular programmin­g languages for developers across the spectrum.

 ??  ??
 ??  ?? Figure 1: Reasons to use Kotlin
Figure 1: Reasons to use Kotlin
 ??  ?? Figure 2: Kotlin – try it out online
Figure 2: Kotlin – try it out online
 ??  ?? Figure 3: Kotlin in Android
Figure 3: Kotlin in Android
 ??  ?? Figure 4: JavaScript transpilin­g
Figure 4: JavaScript transpilin­g

Newspapers in English

Newspapers from India