The choice of a programming language is a crucial factor in the effective implementation of any software project. In the modern multi-platform era, it is important that a language has the capabilities to address the specific needs of the platform being used. Kotlin is an emerging programming language with which you can build applications 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.
Programming languages play an important role in deciding the speed at which the applications are built. Apart from deciding the development time, the optimality of the implementation is also affected by the choice of the programming language. These days, there are many programming languages to choose from. However, there are various factors that determine which language one selects, such as platform support, availability of support libraries, community help, documentation, 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 contributed 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 programming language with a growing developer support base. It is a statically typed programming language, and the licensing associated with it is Apache 2. Its design is influenced by other popular programming languages such as Java, Scala, etc. One of the design philosophies behind Kotlin is to develop a language that combines the feature set of Scala with the compilation speed of Java.
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
- 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 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.
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:
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:
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:
If you want the object-oriented version of Hello World, here is the code snippet:
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):
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
- 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.
- Gradle approach
- IntelliJ IDEA approach
- Maven approach
- Command line approach.
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)
- 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.
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.