Mastering Kotlin

By Nate Ebel
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. A New Challenger Approaches

About this book

Using Kotlin without taking advantage of its power and interoperability is like owning a sports car and never taking it out of the garage. While documentation and introductory resources can help you learn the basics of Kotlin, the fact that it’s a new language means that there are limited learning resources and code bases available in comparison to Java and other established languages.

This Kotlin book will show you how to leverage software designs and concepts that have made Java the most dominant enterprise programming language. You’ll understand how Kotlin is a modern approach to object-oriented programming (OOP). This book will take you through the vast array of features that Kotlin provides over other languages. These features include seamless interoperability with Java, efficient syntax, built-in functional programming constructs, and support for creating your own DSL. Finally, you will gain an understanding of implementing practical design patterns and best practices to help you master the Kotlin language.

By the end of the book, you'll have obtained an advanced understanding of Kotlin in order to be able to build production-grade applications.

Publication date:
October 2019


A New Challenger Approaches

In this chapter, you'll gain an understanding of what Kotlin is, how it came about, and why it's quickly gaining popularity. You'll find a high-level overview of key language features, as well as the design principles behind the language itself. Finally, this chapter will lay the foundations for the following chapters' focus on features, patterns, platforms, and best practices for improving your understanding of the Kotlin programming language.

This chapter covers the following topics:

  • Creating a modern language for the Java Virtual Machine (JVM)
  • Moving beyond the JVM
  • Designing Kotlin with best practices in mind
  • Checking in on the current state of Kotlin

Technical requirements

In order to download, compile, and execute the samples found in this chapter, you must have the following:

  • IntelliJ IDEA 2018.3 Community or Ultimate editions, or newer
  • An internet connection
  • Git and GitHub (optional)

To download all of the code in this chapter, including the examples and code snippets, please refer to the following GitHub link:


Creating a modern language for the JVM

Kotlin was born out of a desire for a modern programming language that could be run on the JVM while still being fully compatible with Java and existing Java tooling. With these goals in mind, Kotlin has evolved into one of the fastest growing programming languages in the world and continues to carve out space for itself across multiple domains.

GitHub's 2018 State of the Octoverse report listed Kotlin as the fastest growing language on GitHub:

In this section, we're going to dive into what Kotlin is, how it came to be, and why it's great for developers.

What is Kotlin?

So, what exactly is Kotlin? Kotlin is a statically typed programming language designed to run on the JVM and to be 100% compatible with Java and existing Java tooling. Kotlin combines a modern set of features that gives it unique advantages over other JVM languages.

Kotlin is flexible

Kotlin supports, but does not strictly enforce, multiple programming paradigms. With Kotlin, you can write object-oriented, functional, imperative, and reactive code, both separately and combined. Kotlin also supports modern features such as type inference, allowing the compiler to worry about enforcing strict typing rather than the developer. Kotlin can also be run on a variety of different platforms, from IoT devices and mobile applications to the browser.

Kotlin is expressive and concise

Kotlin is designed to be both expressive and concise. Features such as type inference and default parameter values enable developers to accomplish their goals with less code while features such as data classes and object classes allow developers to express common patterns such as singletons with a single keyword.

Kotlin is powerful

Although relatively new, Kotlin is a fully featured, powerful programming language ready to tackle the demands of modern software requirements. Features such as higher-order functions, coroutines, and a comprehensive standard library give developers all the tools they need to ship high-quality software.

Hello Kotlin

The following snippet illustrates several interesting features available in Kotlin:

fun formatName(name: String?) = name ?: "Fellow Human"

fun greetReader(greeting: String = "Hey", name: String?) =
println("$greeting ${formatName(name)}")

fun main(args: Array<String>) {
greetReader("Hello!", "Reader")
// Hello! Reader

As you look at these few lines of code, you will notice a few items of interest, which are as follows:

  • The use of the fun keyword to define a new function
  • Functions that exist outside of any enclosing class
  • Demonstrations of null and non-null types
  • Support for default parameter values and String templates

Who created Kotlin?

Kotlin was created by software company JetBrains who are best known for creating excellent development tools such as IntelliJ IDEA. JetBrains has continued to invest in Kotlin over the years and is still a driving force behind the advancement of the language.

Announcing Kotlin

Kotlin was first announced to the world in 2011 at the JVM Language Summit. Having already been in development for a year upon announcement, the first public release came in January 2012. The following month saw the open source release of Kotlin under the Apache License 2.0.

Motivations for Kotlin

According to JetBrains, their motivations for creating an entirely new programming language were threefold:

  • They wanted a more productive language for the JVM than was currently available. Existing solutions such as Java or Scala either lacked modern language features or suffered from slow compile times.
  • They expected Kotlin's adoption to drive sales of IntelliJ.
  • It was hoped that the increased discussion and awareness of the company would lead to greater trust in JetBrains itself and their philosophies around building quality development tools.

Community involvement

From the early days of Kotlin, lead language designer Andrey Breslav made it clear that several things were important in the development of the language such as first-class interoperability and community feedback.

Since the initial announcement of Kotlin, JetBrains has been open about motivations, design decisions, and the development process. This openness has worked in their favor and has contributed to the current success of the Kotlin programming language. Now, let's see what lies beyond all this.


Moving beyond the JVM

Kotlin may owe its origin to JVM interoperability, but it has quickly moved beyond pure JVM applications. One of the early wins for Kotlin was acceptance in the Android development community where most developers were required to use Java 6 or Java 7. Kotlin enabled Android developers to use language features, such as lambdas, which were not available on older versions of Java.

Outside of Android, Kotlin can now be transpiled to JavaScript, used in multiplatform mobile applications, or compiled to run natively on macOS, Windows, and Linux.

Kotlin for Android

To date, Kotlin has received the most popularity in the Android development community. Kotlin starting gaining traction for Android development in 2015, but it was 2017 when Kotlin really came to the forefront. At Google IO 2017, Google announced first-class support for the Kotlin programming language. It was to sit alongside Java and C++ as officially supported languages for the platform and this marked the beginning of the large-scale adoption of the language.

When Android Studio 3.0 was released in October of 2017, there was no longer a major blocker to adopting the language in established projects. Teams that had been concerned about prerelease versions of plugins or IDEs could now try the language on stable tooling with the full, long-term support of Google. This allowed teams and organizations to adopt the language with much more confidence and began the surge in Kotlin's popularity that we see today.

The official Android documentation now defaults to Kotlin when displaying code snippets. This is just one example of Google's commitment to long-term support for Kotlin on Android.

IntelliJ and Android Studio both make it incredibly easy to integrate Kotlin with existing Android projects, or to start new projects that are 100% Kotlin. Additionally, Google continues to invest in Kotlin with improved tooling and the Core-KTX Jetpack library, which makes building Android applications with Kotlin even more enjoyable.

Chapter 13, Kotlin on Android, will dive deeper into the usage of Kotlin for Android application development.

Kotlin for the web

From the beginning, Kotlin has been built with portability in mind. Because it is a JVM language, it can operate anywhere with an existing JVM stack, and with support for transpiling to JavaScript, you can write Kotlin code for a variety of web development needs, such as manipulating the DOM or interacting with Node.js.

Chapter 14, Kotlin and Web Development, will further explore the use of Kotlin in JVM and JavaScript web development.

Now, let's see how Kotlin works here.

Server-side Kotlin

Java remains one of the most popular programming languages in the world and much of that use is happening in server-side backend systems. Kotlin can integrate smoothly with any of these JVM supported systems and can be integrated all at once, or bit by bit as developers become more familiar with the language. Kotlin can be used to deploy these applications to any system that supports Java web applications.

Kotlin tooling for server-side work is great as well. IntelliJ IDEA has full support for the language and there are additional plugins for popular frameworks such as Spring. JetBrains has also developed Ktor, an unopinionated framework for developing web applications with Kotlin. These tools aim to make it as easy as possible to use Kotlin for your server-side work.

Kotlin to JavaScript

Not only can Kotlin be used to build JVM-compatible projects for the web, but it can additionally be transpiled to target client-side and server-side JavaScript. The transpiled code currently targets ECMAScript 5.1 and aim to provide as consistent an experience as possible between targeting the JVM and JavaScript.

Native and multiplatform Kotlin

JetBrains continues to work toward Kotlin becoming a more ubiquitous language, and a big part of that effort is in Kotlin/Native. Kotlin/Native enables developers to write Kotlin code that is compiled to native binaries. This enables Kotlin to be run on platforms such as iOS, macOS, Windows, and so on.

Kotlin/Native retains its great interoperability and can be used to integrate with other languages such as C++ or Objective-C. This allows Kotlin to be used in multiplatform projects where common functionality is written in Kotlin and then shared between other targets. A great example of this is in mobile development where Kotlin code can be shared between iOS and Android applications.

In Chapter 15Introducing Multiplatform Kotlin, you'll learn more about how Kotlin/Native and multiplatform projects can bring your Kotlin code to additional platforms.

Having an understanding of these concepts, let's now understand some best practices with Kotlin in mind.


Designing Kotlin with best practices in mind

Kotlin has been designed based on decades of experience from working with Java and other programming languages. By building on this experience, JetBrains has worked to improve the developer experience with Kotlin by focusing on things such as first-class tooling support from day one, fast build times, and bringing modern features and best practices to the language design.

Learning from Java

Kotlin is 100% compatible with Java through the bytecode that allows both to target the JVM. Kotlin's origin stems from wanting modern language features and compile speeds that other JVM languages couldn't provide. Because of these, Kotlin is heavily influenced by Java, but can improve areas where deficiencies or best practices have been discovered over the years.

Best practice by design

By examining other languages, and common best practices, Kotlin's designers have been able to provide language features and syntax that make it easier to write safer, cleaner, more concise code by default.

A few examples of this include the following:

  • Non-null types
  • First-class support for functions and function types
  • Invariant arrays
  • Language support for singletons
  • Data classes

Long considered to be the billion dollar mistakenull, and how to handle it properly, is one of the most common challenges for Java developers. Kotlin looks to improve on this by making types non-null by default, and it requires developers to explicitly mark something as nullable. 

Kotlin includes first-class support for functions and for function types. This makes Kotlin well suited to functional programming and can allow developers to reduce the number of classes in their projects. Features such as data classes, invariant arrays, and final-by-default classes all help enforce immutability in your code base. 

By making it easier, or the default behavior, to enforce best practices, it makes it more likely that developers will follow them. 

After understanding the best practices for learning Kotlin from Java and by design, we can now check the current state of Kotlin.


Checking in on the current state of Kotlin

Today, Kotlin is one of the fastest growing languages in the world. It has already proven itself for Android development and is now starting to find a home in other domains as well.

Developing Kotlin in the open

Since its public announcement, JetBrains has been very open with the development of Kotlin. This includes a public issue tracker, regular blog posts, conference talks, a yearly Kotlin census, and they even have their own conference now, KotlinConf.

You can submit your feedback, or leave your own issues and feature requests in the Kotlin issue tracker:

Kotlin has seen four major stable releases to date, which are as follows:

  • v1.0: February 15, 2016
  • v1.1: March 1, 2017
  • v1.2: November 28, 2017
  • v1.3: October 29, 2018

Each of these releases has brought exciting new functionality to the language. Most recently, Kotlin v1.3 brought coroutine support for asynchronous programming.

Increasing popularity for Kotlin

The popularity of Kotlin can be seen in a number of ways. GitHub's 2018 The State of the Octoverse survey listed Kotlin as the number one fastest growing language by contributors, seeing a 2.6x increase in contributors over the previous year.

JetBrains is currently planning their third KotlinConf, which has welcomed over 1,200 attendees at each of the earlier events. The reception at the first event was so positive, and demand overseas was so high, that in 2018, the event was moved from San Francisco to Amsterdam.

As more and more developers are turning to Kotlin, we are now seeing an increase in learning tools as well. Companies such as Google and Udacity have partnered to develop Kotlin training courses. JetBrains now offers a Kotlin training certification for individuals or companies that want to certify the quality of their Kotlin instruction. Developers are writing and speaking about Kotlin all over the world at meetups, conferences, and on podcasts, webinars, and so on. Much of this is new over the past two years, and all indicators suggest that this will only increase as more and more organizations and individuals start using Kotlin in their projects.

Learning Kotlin

If you've never worked with Kotlin, there are a number of ways to try it out and to begin learning the language.

If you would like to jump right into using Kotlin in your existing code base, Kotlin is supported by a number of popular IDEs:

  • Android Studio
  • IntelliJ IDEA Community and IntelliJ IDEA Ultimate
  • Eclipse

Android Studio and IntelliJ IDEA both provide support for quickly converting Java to Kotlin and for examining the common generated bytecode. Additionally, both IDEs provide a REPL tool and scratch files that allow you to quickly run individual Kotlin commands or functions independently from the rest of the code base. This can make it very easy to start playing with Kotlin within a familiar tool.

If you would prefer to try Kotlin before downloading an IDE, there are several options for hands-on learning in your web browser:

Each of these will help you gain an understanding of Kotlin features, and extend from the basics up to more complex coding challenges where you can test your understanding.

In this book, we'll work through examples of many of Kotlin's features. We'll work to understand the features on their own, and then learn how they can be used to cleanly architect your applications and implement familiar design patterns. Additional resources for learning Kotlin can be found at the end of this chapter in the Further reading section.



Kotlin is a modern programming language that can be used to build applications across mobile, the web, and native platforms. Since its inception, JetBrains has developed Kotlin to provide an excellent developer and integration experience so Kotlin can be learned gradually and slowly integrated into existing projects. The design and development of the language has been done very much in the open, and you can view the Kotlin issue tracker and submit your own ideas, issues, and feedback to help contribute to the language. Kotlin's popularity continues to increase rapidly, and its ability to target a variety of platforms makes it likely that the popularity trend will continue.

In this book, you'll learn firsthand why Kotlin is growing so rapidly. We'll start by exploring different application architectures, and how to model data and manage state using Kotlin. We'll then explore how Kotlin can be integrated and tested with existing Java code. Advanced topics such as coroutines and functional programming will be examined, and finally, we'll take a look at how Kotlin can be used on different platforms such as Android and the web.

In Chapter 2, Programmers' Multi-Tool - Flexible, Expressive, and Concise, we'll dive into the building blocks of the language. You'll learn more about Kotlin's support for multiple programming paradigms, first-class functions, non-null types, and explore Kotlin's extensive standard library.



  1. Which company started the development of the Kotlin programming language?
  2. When was Kotlin announced to the world?
  3. Who is the lead designer of Kotlin?
  4. What platforms did Kotlin initially target?
  5. Which platforms are currently supported by Kotlin?
  6. For which platform has Kotlin gained the most popularity?
  7. List two factors that have contributed to the rapid growth of Kotlin.

Further reading

About the Author

  • Nate Ebel

    Nate Ebel is a software developer who enjoys building great software and helping others do the same. He has worked with Android since its early days, across a variety of projects, from creative and educational apps, mapping and navigation applications, to the evolution of robotic controllers. Nate has a passion for technology, education, and software development, and enjoys opportunities to combine the three. He enjoys being involved in the Android developer community and is an active contributor through conference speaking, blogging, and event organizing. He is working to help himself and others dream, learn, and create in an effort to positively impact others.

    Browse publications by this author
Book Title
Unlock this full book FREE 10 day trial
Start Free Trial