Kotlin: A Lan­guage for Mod­ern Multi-plat­form Ap­pli­ca­tions

OpenSource For You - - Contents - By: Dr K.S. Kup­pusamy The au­thor is as­sis­tant pro­fes­sor of com­puter sci­ence in the Pondicherry Cen­tral Univer­sity. He has 12+ years of teach­ing and re­search ex­pe­ri­ence in academia and in in­dus­try. He can be reached at kskuppu@gmail.com.

Kotlin is a multi-plat­form pro­gram­ming lan­guage that is con­cise, safe, in­ter­op­er­a­ble and tool-friendly. It is a stat­i­cally-typed pro­gram­ming lan­guage that runs on the Java vir­tual ma­chine and can also be com­piled to the JavaScript source code or can use the LLVM com­piler in­fras­truc­ture.

The choice of a pro­gram­ming lan­guage is a cru­cial fac­tor in the ef­fec­tive im­ple­men­ta­tion of any soft­ware project. In the mod­ern multi-plat­form era, it is im­por­tant that a lan­guage has the ca­pa­bil­i­ties to ad­dress the spe­cific needs of the plat­form be­ing used. Kotlin is an emerg­ing pro­gram­ming lan­guage with which you can build ap­pli­ca­tions be­long­ing to any of the fol­low­ing cat­e­gories—na­tive, browser based, An­droid and JVM based. This ar­ti­cle ex­plores the rich ar­ray of fea­tures that Kotlin of­fers.

Pro­gram­ming lan­guages play an im­por­tant role in de­cid­ing the speed at which the ap­pli­ca­tions are built.

Apart from de­cid­ing the de­vel­op­ment time, the op­ti­mal­ity of the im­ple­men­ta­tion is also af­fected by the choice of the pro­gram­ming lan­guage. These days, there are many pro­gram­ming lan­guages to choose from. How­ever, there are var­i­ous fac­tors that de­ter­mine which lan­guage one se­lects, such as plat­form sup­port, avail­abil­ity of sup­port li­braries, com­mu­nity help, doc­u­men­ta­tion, etc. So let us ex­plore

Kotlin in this con­text.

The Kotlin lan­guage was de­signed by JetBrains, and there are many open source de­vel­op­ers who have con­trib­uted to its evo­lu­tion. It was re­leased in 2011 and its lat­est sta­ble re­lease, ver­sion 1.2.30, came out in March 2018. Kotlin is an ac­tively main­tained pro­gram­ming lan­guage with a grow­ing de­vel­oper sup­port base. It is a stat­i­cally typed pro­gram­ming lan­guage, and the li­cens­ing as­so­ci­ated with it is Apache 2. Its de­sign is in­flu­enced by other pop­u­lar pro­gram­ming lan­guages such as Java, Scala, etc. One of the de­sign philoso­phies be­hind Kotlin is to de­velop a lan­guage that com­bines the fea­ture set of Scala with the com­pi­la­tion speed of Java.

Rea­sons you should use Kotlin

One of the first ques­tions that de­vel­op­ers ask them­selves while se­lect­ing a pro­gram­ming lan­guage is, “Why use this lan­guage?” Here are some im­por­tant rea­sons for why you should use Kotlin.

ƒ It is multi-plat­form: Kotlin can be used to build ap­pli­ca­tions that tar­get the fol­low­ing en­vi­ron­ments:

• Na­tive code

• In-browser ex­e­cu­tion (through JavaScript transcom­pi­la­tion)

• An­droid en­vi­ron­ment

• Java Vir­tual Ma­chine

ƒ It is con­cise: One of the main ad­van­tages of us­ing Kotlin is its abil­ity to re­duce the amount of boil­er­plate code con­sid­er­ably. So de­vel­op­ers can fo­cus on the task at hand, rather than wor­ry­ing about the skele­ton in which the code needs to be placed.

ƒ It is safe: Kotlin has lots of in­built mech­a­nisms to pre­vent a large class of er­rors. One that will be liked by many de­vel­op­ers is ‘null pointer ex­cep­tions’.

ƒ It is in­ter­op­er­a­ble: It is a ma­jor ad­van­tage to be able to use li­braries across JVM, An­droid and JavaScript en­vi­ron­ments.

ƒ It is tool-friendly: For many de­vel­op­ers, the in­te­grated de­vel­op­ment en­vi­ron­ment (IDE) is a fac­tor that af­fects their productivity. Kotlin can be used with many pop­u­lar IDEs (In­tel­liJ IDEA, An­droid Stu­dio, Eclipse, etc). You can in­ter­act with it through the com­mand line as well.

Why Kotlin has been given this name

Kotlin is the name of an is­land near St Peters­burg in Rus­sia. The Kotlin team chose this name since the name ‘Java’ also has a link to an is­land (apart from the con­nec­tion with cof­fee).

With the re­lease of An­droid Stu­dio 3.0, Kotlin is com­pletely sup­ported as a pro­gram­ming lan­guage for the An­droid OS. It can be used as an al­ter­na­tive to the stan­dard Java com­piler.

Get­ting started

The eas­i­est way to get started with Kotlin is to use https:// try.kotlin­lang.org/. The en­vi­ron­ment pro­vided here is in­tu­itive. There are lots of sam­ple pro­grams pro­vided in this en­vi­ron­ment. Let’s first write the ‘Hello World’ pro­gram, as fol­lows:

fun main(args: Ar­ray<String>) { println("Hello, world! Sam­ple for OSFY") }

Af­ter writ­ing this code, se­lect one of the fol­low­ing as the tar­get:


ƒ JavaScript

ƒ JUnit

Then click on the Run icon. The out­put ap­pears in the out­put win­dow.

The in­put can be read from com­mand line ar­gu­ments. A sam­ple code snip­pet is as fol­lows:

fun main(args: Ar­ray<String>) { if (args.size == 0) { println(“Please pro­vide a name as a com­mand-line ar­gu­ment”) re­turn } println(“Hello, ${args[0]}!”) }

It can be ob­served from the above code snip­pet that the semi-colons are op­tional in Kotlin. The new­line is au­to­mat­i­cally in­ferred by Kotlin.

Mul­ti­ple ar­gu­ments can be read from the com­mand line us­ing ‘for’, as shown in the fol­low­ing code snip­pet:

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

If you want the ob­ject-ori­ented ver­sion of Hello World, here is the code snip­pet:

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

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

It may be ob­served that the ‘new’ key­word is not used for cre­at­ing an ob­ject.

If you wish to in­stall the Kotlin com­piler in your sys­tem, try the fol­low­ing com­mand from a ter­mi­nal (for Ubuntu users):

$ sudo snap in­stall --clas­sic kotlin

De­tailed in­struc­tions for com­mand line in­stal­la­tion for other OSs are avail­able at https://kotlin­lang.org/docs/ tu­to­ri­als/com­mand-line.html.

Kotlin in An­droid

The of­fi­cial Kotlin doc­u­men­ta­tion lists the ma­jor fea­tures that Kotlin of­fers for An­droid de­vel­op­ment.

ƒ Com­pat­i­bil­ity: The com­pat­i­bil­ity with JDK 6 en­sures that Kotlin based ap­pli­ca­tions can run on older

An­droid de­vices.

ƒ Kotlin is 100 per cent in­ter­op­er­a­ble with Java: This means that all the ex­ist­ing An­droid li­braries can be used in Kotlin ap­pli­ca­tions.

ƒ A min­i­mal foot­print: This im­por­tant fea­ture is avail­able be­cause Kotlin has a very com­pact run-time li­brary.

If you are a Java de­vel­oper, you can get started with

Kotlin in­stantly. There is an au­to­mated Java-to-Kotlin con­verter avail­able.

Koltin has been used by ma­jor ap­pli­ca­tions such as Pin­ter­est, Base­camp’s An­droid app, etc. For sam­ple An­droid apps built us­ing Kotlin, you can go to https://de­vel­oper. an­droid.com/sam­ples/in­dex.html?lan­guage=kotlin. Google has pro­vided some in­ter­est­ing ap­pli­ca­tions built us­ing Kotlin at this link.

A de­tailed tu­to­rial on get­ting started with Kotlin for An­droid app de­vel­op­ment is avail­able at https://kotlin­lang. org/docs/tu­to­ri­als/kotlin-an­droid.html.

Kotlin in browsers

As stated ear­lier, Kotlin en­ables the de­vel­oper to tran­spile Kotlin code to JavaScript. The Kotlin-to-JavaScript com­piler en­sures that the code gen­er­ated is optimal in size, and read­abil­ity is also en­sured.

Some of the prime rea­sons for tar­get­ing JavaScript tran­spiling are given be­low.

ƒ DOM in­ter­ac­tion: With the fea­tures pro­vided by Kotlin, DOM can be han­dled. It is pos­si­ble to up­date ex­ist­ing el­e­ments and cre­ate new el­e­ments too.

ƒ The graph­ics in­ter­ac­tion can be man­aged through We­bGL: This can be used to build graph­i­cal com­po­nents in the Web pages.

Kotlin can be adopted for browser-side use along with other JavaScript li­braries or frame­works such as jQuery or Re­act.

The con­ver­sion from Kotlin to JavaScript can be done in mul­ti­ple ways. The of­fi­cial doc­u­men­ta­tion has listed the fol­low­ing meth­ods:

ƒ Gra­dle ap­proach

ƒ In­tel­liJ IDEA ap­proach

ƒ Maven ap­proach

ƒ Com­mand line ap­proach.

In the ‘Kotlin – Try on­line’ tu­to­rial, the ‘Hello World’ ex­am­ple pro­duces the fol­low­ing JavaScript code:

kotlin.kotlin.io.out­put.flush(); if (typeof kotlin === ‘un­de­fined’)

{ throw new Er­ror(“Er­ror load­ing mod­ule ‘mod­uleId’. Its de­pen­dency ‘kotlin’ was not found. Please, check whether ‘kotlin’ is loaded prior to ‘mod­uleId’.”);

} var mod­uleId = func­tion (_, Kotlin)

{ ‘use strict’; var println = Kotlin.kotlin.io.print­l­n_s8jyv4$; func­tion main(args)

{ println(‘Hello, world! Sam­ple for OSFY’); } _.main_kand9s$ = main; main([]); Kotlin.de­fineMo­d­ule(‘mod­uleId’, _); re­turn _; }(typeof mod­uleId === ‘un­de­fined’ ? {} : mod­uleId, kotlin); kotlin.kotlin.io.out­put.buf­fer;

The de­tailed in­struc­tions on how to han­dle the afore­men­tioned ap­proaches are given at https://kotlin­lang. org/docs/tu­to­ri­als/javascript/kotlin-to-javascript/kotlin-to­javascript.html.

Na­tive Kotlin

Kotlin has a fea­ture for com­pil­ing to na­tive bi­na­ries, and can ex­e­cute this fea­ture with­out any vir­tual ma­chine. The tar­get plat­forms that na­tive Kotlin sup­ports are listed be­low: ƒ Win­dows (x86_64)

ƒ Linux ƒ iOS

ƒ An­droid

ƒ Web As­sem­bly

Kotlin on the server-side

Kotlin can be used for build­ing server-side ap­pli­ca­tions as well. The fol­low­ing are some of the key ad­van­tages of us­ing Kotlin on the server-side.

ƒ Scal­a­bil­ity: Kotlin has the po­ten­tial to han­dle a mas­sive num­ber of clients.

ƒ Mi­gra­tion sup­port: There is in­built sup­port for mi­grat­ing large code bases from Java to Kotlin.

For server-side de­vel­op­ment, there are many im­por­tant frame­works like Vert.x and Ktor.

Kotlin’s abil­ity to sup­port large server-side ap­pli­ca­tions is al­ready proven with some ma­jor im­ple­men­ta­tions. For ex­am­ple, Corda, which is an open source dis­trib­uted ledger plat­form sup­ported by ma­jor banks, is built com­pletely with Kotlin.

Java in­ter­op­er­abil­ity

As stated ear­lier, Kotlin sup­ports in­ter­op­er­abil­ity with Java. The ex­ist­ing Java code can be called from Kotlin in a seam­less man­ner. A com­par­a­tive anal­y­sis of Kotlin and Java is pro­vided at https://kotlin­lang.org/docs/ref­er­ence/ com­par­i­son-to-java.html. Some of the prom­i­nent ad­van­tages of Kotlin are listed be­low: ƒ Has a null safety fea­ture

ƒ Has ex­ten­sion func­tions

ƒ Op­er­a­tor over­load­ing is al­lowed

ƒ Im­ple­ments corou­tines

The of­fi­cial doc­u­men­ta­tion claims that Kotlin code is more com­pact than Java code. In fact, it shows an es­ti­mated 40 per cent re­duc­tion in the num­ber of lines of code with Kotlin (https://kotlin­lang.org/docs/ref­er­ence/ faq.html).

To know more about Kotlin, re­fer to the many books avail­able. A list is given at https://kotlin­lang.org/docs/ books.html.

With its multi-plat­form sup­port and con­cise code, Kotlin surely has the po­ten­tial to evolve into one of the most pop­u­lar pro­gram­ming lan­guages for de­vel­op­ers across the spectrum.

Fig­ure 1: Rea­sons to use Kotlin

Fig­ure 2: Kotlin – try it out on­line

Fig­ure 3: Kotlin in An­droid

Fig­ure 4: JavaScript tran­spiling

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.