Developers

A complete guide on getting started with Kotlin [Cheat Sheet Included]

What is Kotlin?

Kotlin is an OSS statically typed programming language that targets the JVM, Android, JavaScript, and Native language. It is developed by JetBrains. The project started in 2010 and was open source from its initial days. The first official 1.0 release was in February 2016. Kotlin does not aim to be unique but it draws inspiration from decades of language development. It exists in variants that target the JVM (Kotlin/JVM), JavaScript (Kotlin/JS), and Native code (Kotlin/Native).

Why Kotlin?

Kotlin provides some powerful features that tend to be extremely beneficial and also significantly improves the productivity of developers.

Concise: Kotlin drastically reduces the amount of boilerplate code. The fewer lines of code mean that you spend less time to write, read, and debug the code. The best example to demonstrate conciseness is the way of creating a class (also known as POJO for Java users) with the getters, setters, equals(), hashCode(), toString(), and copy() methods. This operation can be performed by using the following line of code:

data class User(val name: String, val email: String, val address: String)

Null safe: The NullPointerException exceptions are extremely infuriating for developers. The Kotlin’s type system is aimed to eliminate the occurrence of NullPointerException from every code. It distinguishes between references that can hold null (known as nullable references) and those that cannot hold null values (known as non-null references). Therefore, Kotlin protects you from mistakenly operating on nullable types.

val name: String? = null // Nullable type

println(name.length()) // Compilation error

Interoperable- Kotlin allows you to use any existing libraries on the JVM because it provides 100% compatibility and SAM support. It is also possible to target either the JVM or JavaScript. You can write code in Kotlin and decide where to deploy. For instance:

import kotlin.browser.window

fun onLoad() {
     window.document.body!!.innerHTML += “<br/>Hello, Kotlin!”
}

Extensions: Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done by using special declarations called extensions. For example, you can write new functions for a class from an external library that you cannot modify. These types of functions can be called in the usual way if they are the methods of the original class. This mechanism is called extension functions. There are also extension properties that allow you to define new properties for existing classes.

Kotlin is highly tool-friendly in terms of functionality. It offers coroutines, high-order functions, lambdas and much more.

What can Kotlin be used for?

Kotlin can be used for any kind of development. These developments can be in the server-side system, client-side web, and Android OS. Due to the proper usage of Kotlin/Native in the systems, the support for other platforms such as embedded systems, macOS, and iOS is also available.

Kotlin is mainly used to develop server-side applications that allow you to write concise and expressive code while maintaining full compatibility with existing Java-based technology stacks and a smooth learning curve. Some of the frameworks that are used for server-side development by using Kotlin are Spring, Vert.x, Ktor, etc.

Kotlin is supported as a first-class language on Android. It provides seamless integration and makes Android development much easier. Kotlin is fully compatible with JDK 6 and its later versions. This ensures that Kotlin applications can run on older Android devices without any errors. Kotlin Android Extensions is a compiler extension that allows you to eliminate findViewById()calls in your code and replaces them with synthetic compiler-generated properties. There are many applications that already use Kotlin for Android, such as Basecamp, Pinterest and more.

Kotlin also provides the ability to target JavaScript by transpiling Kotlin to JavaScript. The current implementation targets ECMAScript 5.1. Kotlin can be used together with existing external libraries and frameworks such as jQuery or React.

In addition to using for backend web, Kotlin/JS can be used for client-side web. Kotlin/JS can be used for backend and client-side web. It uses definitions from DefinitelyTyped to get static typing for common JavaScript libraries. It is compatible with existing module systems such as AMD and CommonJS.

Kotlin/Native is a technology for compiling Kotlin code to native binaries that can run without a virtual machine. It is an LLVM-based backend for the Kotlin compiler and native implementation of the Kotlin standard library. Kotlin/Native is primarily designed to allow compilation for platforms where virtual machines cannot be used, for example, embedded devices or iOS. It helps developers produce a self-contained program that does not require an additional runtime or virtual machines.

Kotlin is also being adopted to the world of data science. Data scientists use development tools called notebooks. Notebooks let users conduct research and store it in a single environment. In a notebook, you can write narrative text next to a code block, execute the code block, and see the results in any format that you require such as output text, tables, data visualization, and so on. Kotlin provides integration with two popular notebooks, Jupyter and Apache Zeppelin. These notebooks allow you to write and run Kotlin code blocks.

How to start with Kotlin?

The best way to begin with Kotlin is to go through the official documentation. It covers all the concepts starting from basic syntax to different uses of Kotlin. All the updates are also regularly posted there. It might seem a bit overwhelming at first but we recommend you to read it step-by-step. You must start by setting up Kotlin in your preferred IDE or command line. To set up the environment, follow the Getting Started section of the document. After that, read about the basic syntax, variables, methods, and classes.

The Kotlin website also provides an online sandbox to explore the language.

Learn Kotlin by Example is a great way to learn as it provides a set of small and simple annotated examples for beginners. It also provides examples to demonstrate variables, functions, null safety, classes, control flow, collections, and much more. It also covers productivity boosters and Kotlin/JS.

After getting a basic idea about Kotlin, start coding on Kotlin Playground.

Kotlin Koans is a series of exercises that you get familiar with the Kotlin syntax and some idioms. Each exercise is created as a failing unit test and your job is to make it pass.

Kotlin Hands-On is a series of hands-on labs where you can create applications with Kotlin using a variety of different technologies and targeting multiple platforms. Currently, there are labs for building web applications with React and Kotlin/JS, Introduction to Coroutines and Channels, Introduction to Kotlin Multiplatform, Introduction to Kotlin/Native, Targeting iOS and Android with Kotlin Multiplatform, and The State of Kotlin/JS.

Apart from them, there are already a number of books available for Kotlin, including Kotlin in Action that is contributed by Kotlin team members Dmitry Jemerov and Svetlana Isakova, Kotlin for Android Developers targeted at Android developers. There are a few courses available for Kotlin, including a Coursera course by Andrey Breslav and Svetlana Isakova, Pluralsight Kotlin Course by Kevin Jones, and an Udemy Kotlin course by Peter Sommerhoff.

The recordings of KotlinConf’19 talks are available on YouTube. KotlinConf is an annual conference dedicated entirely to Kotlin. Other videos can be found on the JetBrains TV YouTube channel.

Globant and JetBrains bring #TheUltimateKotlinChallenge. Create an app using Kotlin, send ur GitHub repo link and a video of the app. Last date of submission Friday, May 15, 2020. More details here – https://bit.ly/2xS8Iv4

Finally, do participate in the Kotlin Consensus as it is aimed to help everyone in the community and bring their ideas to life with Kotlin.

Let’s Kotlin!

Please note: To download the gist of the blog, click here.

Rishabh Bansal

Rishabh is the community manager at InterviewBit and is the organizer of Kotlin Delhi User Group. He is helping the next generation of tech leaders create inclusive communities. He strongly believes that the hacker culture provides various technological incentives to the students, and hence supports student hackers through both his professional work and community leadership. An avid supporter of diversity in technology, Rishabh has also worked with ed-tech startups like Coding Blocks in the past. He has impacted over 5000 students in India and continues to do so.

Share
Published by
Rishabh Bansal

Recent Posts

Top 10 HR Competencies to Build a Strong HR Department: A Comprehensive Guide

Introduction In today's dynamic workplaces, a strong HR department is no longer a luxury –…

5 hours ago

8 Steps for Conducting a Job Tasks Analysis: A Complete Guide

Job task analysis is a crucial process for understanding the specific duties and skills required…

5 hours ago

Top 8 Sourcing Tools for Recruiters: A Comprehensive Guide

In today's competitive talent landscape, attracting top candidates requires going beyond traditional job board postings.…

6 hours ago

The 12 Most Effective Employee Selection Methods: A Comprehensive Guide

Finding the perfect fit for your team can feel like searching for a unicorn. But…

6 hours ago

12 Important Recruiting Metrics You Should Know

Recruitment forms a strong foundation to build an effective team. However, do you know if…

9 hours ago

7 Modern Performance Appraisal Methods to Boost Workforce Development

Introduction Performance appraisal has seen a tremendous change over the years. It is no longer…

1 day ago