Skip to product information
Kotlin Cookbook
Kotlin Cookbook
Description
Book Introduction
The fastest way for developers familiar with Java to learn Kotlin is all contained in this book's 85 practical recipe examples.
It provides a way to write code that is concise, safe, and more Kotlin-like in various situations encountered during development.
You can easily learn how to apply Kotlin, a Java-based language, to your own projects, including Kotlin object-oriented programming, functional programming, collections, sequences, scoped functions, Kotlin delegates, testing, how to set up Spring Boot with Kotlin, coroutines, and structured concurrency.
The practical recipes in this book are invaluable to both experienced developers and beginners new to Kotlin.
  • You can preview some of the book's contents.
    Preview

index
Chapter 1: Installing and Running Kotlin

Recipe 1.1 Running Kotlin Without a Local Compiler
Recipe 1.2 Installing Kotlin on Your Local Computer
Recipe 1.3 Compiling and Running Kotlin from the Command Line
Recipe 1.4 Using the Kotlin REPL
Recipe 1.5 Running a Kotlin Script
Recipe 1.6 Building Standalone Applications with GraalVM
Recipe 1.7 Adding the Kotlin Plugin to Gradle (Groovy Syntax)
Recipe 1.8 Adding the Kotlin Plugin to Gradle (Kotlin Syntax)
Recipe 1.9 Building Kotlin Projects with Gradle
Recipe 1.10 Using Maven with Kotlin

Chapter 2: Kotlin Basics

Recipe 2.1: Using Nullable Types in Kotlin
Recipe 2.2 Adding Null-Tolerance Indicators to Java
Recipe 2.3 Method Duplication for Java
Recipe 2.4 Explicitly Converting Types
Recipe 2.5 Outputting to a Different Base
Recipe 2.6 Raising Numbers to Exponents
Recipe 2.7 Using Bit Shift Operators
Recipe 2.8 Using Bitwise Boolean Operators
Recipe 2.9 Creating a Pair Instance with

Chapter 3 Kotlin Object-Oriented Programming

Recipe 3.1 Understanding the Difference Between Const and Val
Recipe 3.2 Creating Custom Getters and Setters
Recipe 3.3 Defining Data Classes
Recipe 3.4 Support Attribute Techniques
Recipe 3.5 Operator Duplication
Recipe 3.6 Using lateinit for later initialization
Recipe 3.7: Using Safe Type Conversion, Reference Equality, and Elvis to Redefine Equals
Recipe 3.8 Creating a Singleton
Recipe 3.9: A Ruckus About Nothing

Chapter 4 Functional Programming

Recipe 4.1 Using folds in algorithms
Recipe 4.2: Shortening using the reduce function
Recipe 4.3 Applying Tail Recursion

Chapter 5 Collection

Recipe 5.1 Handling Arrays
Recipe 5.2 Creating a Collection
Recipe 5.3 Creating a Read-Only View from a Collection
Recipe 5.4 Creating a Map from a Collection
Recipe 5.5 Returning a Default Value if a Collection is Empty
Recipe 5.6 Restricting Values ​​to a Given Range
Processing Recipe 5.7 Collections with Windows
Recipe 5.8 Decomposing List Structures
Recipe 5.9 Sorting by Multiple Attributes
Recipe 5.10 Defining a Custom Iterator
Filtering collections by Recipe 5.11 type
Recipe 5.12 Creating a Range as a Sequence

Chapter 6 Sequence

Recipe 6.1 Using Delayed Sequences
Recipe 6.2 Creating a Sequence
Recipe 6.3 Handling Infinite Sequences
Recipe 6.4 Yielding in Sequence

Chapter 7 Domain Functions

Recipe 7.1 Initializing an object after creating it with apply
Recipe 7.2 Also use for side effects
Recipe 7.3 Using the let function and the Elvis operator
Recipe 7.4 Using let as a temporary variable

Chapter 8: Kotlin Delegates

Recipe 8.1: Implementing Composition Using Delegates
Recipe 8.2 Using Lazy Delegates
Recipe 8.3 Preventing Values ​​from Being Null
Recipe 8.4 Using Observable and Vetoable Delegates
Recipe 8.5: Providing a Map as a Delegate
Recipe 8.6 Creating a Custom Delegate

Chapter 9 Test

Recipe 9.1 Setting up the Test Class Lifecycle
Recipe 9.2 Using Data Classes in Tests
Recipe 9.3 Using Helper Functions with Default Arguments
Recipe 9.4 Repeating JUnit 5 Tests on Multiple Datasets
Recipe 9.5 Using the data class in parameterized tests

Chapter 10 Input/Output

Managing Resources with Recipe 10.1 use
Recording Recipe 10.2 in File

Chapter 11 Other Kotlin Features

Recipe 11.1: Finding the Kotlin Version
Recipe 11.2 Running Lambda Repeatedly
Recipe 11.3: Forcing the Perfect When
Recipe 11.4 Using the replace function with regular expressions
Recipe 11.5 Converting to and from a binary string
Recipe 11.6 Creating an Executable Class
Recipe 11.7 Measuring Elapsed Time
Recipe 11.8 Thread Start
Recipe 11.9: Force Completion with TODO
Recipe 11.10 Understanding Random Behavior
Recipe 11.11 Using Special Characters in Function Names
Recipe 11.12: Notifying Java of Exceptions

Chapter 12 Spring Framework

Opening a Spring-managed bean class for recipe 12.1 extension
Recipe 12.2: Implementing Persistence with Kotlin Data Classes
Recipe 12.3 Dependency Injection

Chapter 13: Coroutines and Structured Concurrency

Recipe 13.1 Choosing a Coroutine Builder
Recipe 13.2: Replacing async/await with withContext
Recipe 13.3 Using the Dispatcher
Recipe 13.4 Running Coroutines in a Java Thread Pool
Recipe 13.5: Canceling a Coroutine
Recipe 13.6 Debugging Coroutines

Detailed image
Detailed Image 1

Publisher's Review
[Structure of this book]

This book consists of many recipes, each standalone but most related to other recipes.
The order in which you read the book is up to you, but the topics covered in each chapter are as follows:

- Chapter 1, 'Installing and Running Kotlin' covers the basics of installing and running Kotlin, including how to use the REPL, how to use build tools like Maven and Gradle, and how to use the native image generator included in Graal.

- Chapter 2, 'Kotlin Basics', covers some of Kotlin's basic features, such as nullable types, operator overloading, and type conversion, before covering more complex issues, such as bitwise shift operators or how to use the to extension function of the Pair class.

- Chapter 3, 'Kotlin Object-Oriented Programming', focuses on Kotlin's object-oriented features that developers coming from other languages ​​might find surprising or unique.
This chapter explains how to use the const keyword, how Kotlin handles backing properties, lazy initialization, and the dreaded Nothing class, which is sure to confuse existing Java developers.

- Chapter 4, 'Functional Programming', contains only a few recipes that use functional features that require explanation of those features.
Functional programming concepts are discussed throughout this book, particularly when discussing collections, sequences, and coroutines, but this chapter describes a few functional techniques that you might find unusual or particularly interesting.

- Chapter 5, 'Collections', mainly covers methods that are not intuitive to use, such as arrays and collections, decomposing collections, sorting using multiple properties, using windows in collections, and creating sequences.

- Chapter 6, 'Sequence', shows how Kotlin handles sequence items using lazy evaluation, similar to how Java uses streams.
The recipes in this chapter cover creating sequences, yielding from sequences, and using infinite sequences.

- Chapter 7, 'Scope Functions', covers another unique topic in Kotlin: functions that execute blocks of code within an object context.
Kotlin's functions like let, apply, and also are very useful.
This chapter explains in detail how and why to use these functions.

- Chapter 8, 'Kotlin Delegates' explains convenient features of Kotlin, such as how to implement delegates.
Delegates use composition rather than inheritance, and several delegates, such as lazy, observable, and vetoable, are included in the Kotlin core library.

- Chapter 9, 'Testing', covers the important topic of testing, with a particular focus on Junit 5.
The current version of Junit is designed to work well with both plain Kotlin applications and Kotlin Spring Framework applications.
This chapter covers several approaches that make it easier to write and run tests.

- Chapter 10, 'Input/Output', covers two specific recipes related to resource management.
This chapter covers handling file input/output with the use function, which has wide application in many contexts.

- Chapter 11, 'Other Kotlin Features', covers topics that cannot be easily included in any other category.
Topics covered include how to get the current Kotlin version, how to force a when statement even if it doesn't return a value, and how to use the replace function with regular expressions.
Additionally, the TODO function and the Random class are explained.
You will also learn how to integrate Kotlin functions with Java exception handling.

- Chapter 12, 'Spring Framework' covers the Spring Framework along with Spring Boot, which is very friendly to Kotlin.
This chapter contains several recipes that show how to use Kotlin classes as Spring-managed beans, how to implement JPA persistence, and how to inject dependencies when needed.

- Chapter 13, 'Coroutines and Structured Concurrency', covers the topic of coroutines, one of Kotlin's most popular features and the foundation of concurrent and parallel programming in the Kotlin language.
The recipes in this chapter cover basics like builders and dispatchers, how to cancel and debug coroutines, and how to run coroutines on a custom Java thread pool.


[For whom this book is intended]
Beginners who are learning Kotlin for the first time but want to learn it in a more practical way, and Kotlin users who have already started but want to learn advanced techniques.
It is especially helpful for developers who have a basic understanding of object-oriented programming in Java or other JVM-based languages.



[Author's Note]

Welcome to the Kotlin Cookbook.
The primary purpose of this book is not only to teach Kotlin syntax and semantics, but also to show when and why to use specific Kotlin features.
To achieve this goal, it is not necessary to cover all of Kotlin's syntax and libraries in detail.
However, many recipes based on the basic principles have been added, making the book easy to understand even for Kotlin beginners.

There's a strong push from JetBrains to encourage the Kotlin community to embrace multiplatform, native, JavaScript development.
However, since multiplatform, native, and JavaScript development with Kotlin is either in beta or has very low adoption, I ultimately decided not to include multiplatform, native, and JavaScript in the recipe.
As a result, this book focuses solely on Kotlin for the JVM.

You can find all the code for this book in the GitHub repository https://github.com/kousen/kotlin-cookbook.
The code includes a Gradle wrapper (as well as a build file written in the Kotlin DSL) and passes all tests.

All code examples in this book have been compiled and tested on readily available Long Term Support versions of Java, namely Java 8 and Java 11.
Although Java 8 is technically out of date, it's still widely used across the industry, with code examples still available.
At the time of writing this book, the latest version of Kotlin was 1.3.50, with 1.3.60 in progress.
All code will work on both versions, and the GitHub repository will be continuously updated with the latest versions of Kotlin.

Recipe books like this one focus on being as comprehensive as possible about Kotlin, rather than focusing on Kotlin techniques or grammar.
This has the advantage of allowing all recipes in this book to fully utilize Kotlin's features, but also the disadvantage of not spending much time explaining the basics of the Kotlin features used.
Even if you're not familiar with how to create collections, use arrays, or design classes, don't worry, each chapter includes a summary of the basic usage.
This book also moves on to implementing functions in Kotlin libraries.
This shows developers how they actually use the language and why they implemented it that way.


[Translator's Note]
As a programmer, you are always interested in new programming languages.
I also enjoy trying out new programming languages, so I first encountered Kotlin around 2013 when I heard that JetBrains was using it in production.
At the time, I tried following the official Kotlin tutorial, but I was very disappointed because it didn't have any unique features compared to Groovy or Scala. I dismissed Kotlin as a language for sales and never looked at it again. I thought I would never look at Kotlin again after that.

However, public interest in Kotlin reached its peak when the Android team designated Kotlin as the official development language for Android at Google I/O in 2017.
Additionally, I dismissed Eric Meyer, whom I respect, as mere lip service when he said he liked Kotlin at a conference in 2017. However, Eric Meyer continued to show his affection for Kotlin, posting Kotlin code and opinions on Twitter, and thus my indifference gradually turned into interest.

After looking at Kotlin 1.2 in 2018, I saw that there were so many improvements and that its compatibility with Java was also excellent, so I thought that if I were to do a JVM-based project in the future, I would never choose Java again.
Today, Kotlin is not limited to mobile development, but is trying to enter all areas of development, including server-side, JavaScript, native, data analytics, and machine learning. You can find information about these areas in the official Kotlin documentation.

Let's talk more about server-side Kotlin. Spring, the world's most popular framework, has officially designated Kotlin as a supported language since version 5.0, and Gradle, the most popular build automation tool for the JVM, also began supporting the Kotlin DSL starting with version 5.0.
In this way, Kotlin is steadily expanding its territory and I believe it will be a promising language in the future.

Although this book focuses on server-side Kotlin among the many applications of Kotlin, it is definitely a good book for acquiring the basic knowledge that will help you advance in that field.
For existing Java developers, this book allows them to quickly learn the parts they need, compared to the numerous reference books available.
Additionally, even beginners to Kotlin can easily install the Kotlin compiler by following the book and progress to more difficult topics step by step.

I'm currently using Kotlin Spring at my current company, and I had quite a bit of trouble setting up the project for the first time.
This book also contains recipes explaining 'How to build a Spring project with Kotlin', and I was very disappointed that the translation of this book had not been done sooner, as I could have avoided the difficulties I experienced while building the project.

I tried my best, but there may be some shortcomings.
Nevertheless, I hope that readers will be able to learn Kotlin comfortably without having to worry about translation.
GOODS SPECIFICS
- Date of issue: June 10, 2020
- Page count, weight, size: 276 pages | 185*240*16mm
- ISBN13: 9791189909147
- ISBN10: 1189909146

You may also like

카테고리