Mastering Kotlin
eBook - ePub

Mastering Kotlin

Learn advanced Kotlin programming techniques to build apps for Android, iOS, and the web

Nate Ebel

Share book
  1. 434 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Kotlin

Learn advanced Kotlin programming techniques to build apps for Android, iOS, and the web

Nate Ebel

Book details
Book preview
Table of contents
Citations

About This Book

Explore popular language features, Java to Kotlin interoperability, advanced topics, and practical applications by building a variety of sample projects

Key Features

  • Understand and leverage the syntax, tools, and patterns by writing code in Kotlin
  • Explore practical topics such as Java interop, concurrency with coroutines, and functional programming
  • Discover how to use Kotlin for build targets like Android, iOS, JavaScript, and backend service

Book Description

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.

What you will learn

  • Model data using interfaces, classes, and data classes
  • Grapple with practical interoperability challenges and solutions with Java
  • Build parallel apps using concurrency solutions such as coroutines
  • Explore functional, reactive, and imperative programming to build flexible apps
  • Discover how to build your own domain-specific language
  • Embrace functional programming using the standard library and Arrow
  • Delve into the use of Kotlin for frontend JavaScript development
  • Build server-side services using Kotlin and Ktor

Who this book is for

If you're a Kotlin developer looking to further their skills or a professional Java developer looking for better or professional resources in order to make a switch to Kotlin, this book is for you. Familiarity with Kotlin programming will assist with understanding key concepts covered in the book.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering Kotlin an online PDF/ePUB?
Yes, you can access Mastering Kotlin by Nate Ebel in PDF and/or ePUB format, as well as other popular books in Informatik & Programmierung in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781838552367

Section 1: Kotlin – A Modern Solution to Application Development

Kotlin is a modern language for modern-day application development. It builds upon decades of experience with Java and modern influences to provide a programming experience that is powerful, flexible, and delightful. In this part, you'll learn about the Kotlin programming language, its goals, its features, and why it's one of the fastest-growing programming languages in the world.
This section comprises the following chapters:
  • Chapter 1, A New Challenger Approaches
  • Chapter 2, Programmers' Multi-Tool – Flexible, Expressive, and Concise

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: https://github.com/PacktPublishing/Mastering-Kotlin/tree/master/Chapter01.

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: https://github.blog/2018-11-15-state-of-the-octoverse-top-programming-languages/.
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...

Table of contents