Mastering Kotlin
eBook - ePub

Mastering Kotlin

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

Nate Ebel

Partager le livre
  1. 434 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Mastering Kotlin

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

Nate Ebel

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Mastering Kotlin est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Mastering Kotlin par Nate Ebel en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatique et Programmation en Java. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2019
ISBN
9781838552367
Édition
1

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 des matiĂšres