Figure 2: Kotlin – try it out online
Reasons you should use Kotlin
One of the first questions that developers ask themselves while selecting a programming 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 applications that target the following environments:
Native code
In-browser execution (through JavaScript trans-compilation)
Android environment
Java Virtual Machine
It is concise: One of the main advantages of using Kotlin is its ability to reduce the amount of boilerplate code considerably. 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 interoperable: It is a major advantage to be able to use libraries across JVM, Android and JavaScript environments.
It is tool-friendly: For many developers, the integrated development environment (IDE) is a factor that affects their productivity. 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 programming language for the Android OS. It can be used as an alternative to the standard Java compiler.
Figure 3: Kotlin Android
Figure 4: JavaScript transpiling
Getting started
The easiest way to get started with Kotlin is to use https://try.kotlinlang.org/ . The environment provided here is intuitive. There are lots of sample programs provided in this environment. 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:
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 automatically 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 instructions for command line installation for other OSs are available at https://kotlinlang.org/docs/tutorials/command-line.html.
Kotlin in Android
The official Kotlin documentation lists the major features that Kotlin offers for Android development.
Compatibility: The compatibility with JDK 6 ensures that Kotlin based applications can run on older Android devices.
Kotlin is 100 per cent interoperable with Java: This means that all the existing Android libraries can be used in Kotlin applications.
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 applications 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 interesting applications built using Kotlin at this link.
A detailed tutorial on getting started with Kotlin for Android app development 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 readability is also ensured.
Some of the prime reasons for targeting JavaScript transpiling are given below.
DOM interaction: With the features provided by Kotlin, DOM can be handled. It is possible to update existing elements and create new elements too.
The graphics interaction 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 documentation 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.defineModule(『moduleId』, _); return _; }(typeof moduleId === 『undefined』 ? {} : moduleId, kotlin); kotlin.kotlin.io.output.buffer;
The detailed instructions on how to handle the aforementioned approaches are given at https://kotlinlang.org/docs/tutorials/javascript/kotlin-to-javascript/kotlin-to-javascript.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 applications as well. The following are some of the key advantages of using Kotlin on the server-side.
Scalability: 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 development, there are many important frameworks like Vert.x and Ktor.
Kotlin』s ability to support large server-side applications is already proven with some major implementations. For example, Corda, which is an open source distributed ledger platform supported by major banks, is built completely with Kotlin.
Java interoperability
As stated earlier, Kotlin supports interoperability with Java. The existing Java code can be called from Kotlin in a seamless manner. A comparative 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 overloading is allowed
Implements coroutines
The official documentation 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 programming languages for developers across the spectrum.