Kotlin in Action
Dmitry Jemerov and Svetlana Isakova
Foreword by Andrey Breslav
  • February 2017
  • ISBN 9781617293290
  • 360 pages
  • printed in black & white
ePub + Kindle available Feb 24, 2017

Explains high-level concepts and provides all the necessary details as well.

From the Foreword by Andrey Breslav, Lead Designer of Kotlin

Kotlin in Action guides experienced Java developers from the language basics of Kotlin all the way through building applications to run on the JVM and Android devices.

Table of Contents detailed table of contents

Part 1: Introduction

1. Kotlin: What and Why

1.1. A Taste of Kotlin

1.2. Kotlin's Main Traits

1.2.1. Target Platforms

1.2.2. Statically Typed

1.2.3. Functional and Object-Oriented

1.2.4. Free and Open-Source

1.3. The Applications of Kotlin

1.3.1. Kotlin on the Server Side

1.3.2. Kotlin on Android

1.4. The Philosophy of Kotlin

1.4.1. Pragmatic

1.4.2. Concise

1.4.3. Safe

1.4.4. Interoperable

1.5. Using the Kotlin Tools

1.5.1. Compiling Kotlin Code

1.5.2. Plugin for IntelliJ IDEA and Android Studio

1.5.3. Interactive Shell

1.5.4. Eclipse Plugin

1.5.5. Online Playground

1.5.6. Java to Kotlin Converter

1.6. Summary

2. Kotlin basics

2.1. Basic Elements

2.1.1. Hello, world!

2.1.2. Easier String Formatting: String Templates

2.1.3. Functions

2.1.4. Variables

2.2. Classes and Properties

2.2.1. Kotlin Source Code Layout

2.2.2. Properties

2.2.3. Kotlin source code layout: directories and packages

2.3. Representing and handling choices: enums and "when"

2.3.1. Declaring enum classes

2.3.2. Using "when" to deal with enum classes

2.3.3. Using "when" with arbitrary objects

2.3.4. Using “when” without an argument

2.3.5. Smart casts: combining type checks and casts

2.3.6. Refactoring: replacing "if" with "when"

2.3.7. Blocks as Branches of "if" and when

2.4. Iterating over Things: 'while' and 'for' Loops

2.4.1. The while Loop

2.4.2. Iterating over numbers: ranges and progressions

2.4.3. Iterating over maps

2.4.4. Using "in" to check collection and range membership

2.5. Exceptions in Kotlin

2.5.1. 'try', 'catch' and 'finally'

2.5.2. 'try' as an expression

2.6. Summary

3. Defining and calling functions

3.1. Creating collections in Kotlin

3.2. Making functions easier to call

3.2.1. Named arguments

3.2.2. Default parameter values

3.2.3. Getting rid of static utility classes: top-level functions and properties

3.3. Adding methods to other people's classes: extension functions and properties

3.3.1. Imports and extension functions

3.3.2. Calling extension functions from Java

3.3.3. Utility functions as extensions

3.3.4. No overriding for extension functions

3.3.5. Extension properties

3.4. Working with collections: varargs, infix calls, and library support

3.4.1. Extending the Java Collections API

3.4.2. Varargs: functions that accept an arbitrary number of arguments

3.4.3. Working with pairs: infix calls and destructuring declarations

3.5. Working with strings and regular expressions

3.5.1. Splitting strings

3.5.2. Regular expressions and triple-quoted strings

3.5.3. Multiline triple-quoted strings

3.6. Making your code tidy: local functions and extensions

3.7. Summary

4. Classes, objects, and interfaces

4.1. Defining class hierarchies

4.1.1. Interfaces in Kotlin: methods with default implementations

4.1.2. Open, final, and abstract modifiers: final by default

4.1.3. Visibility modifiers: public by default

4.1.4. Inner And nested classes: nested by default

4.1.5. Sealed classes: defining restricted class hierarchies

4.2. Declaring a class with nontrivial constructors or properties

4.2.1. Initializing classes: primary constructor and initializer blocks

4.2.2. Secondary constructors: initializing the superclass in different ways

4.2.3. Implementing properties declared in interfaces

4.2.4. Accessing a backing field from a getter or setter

4.2.5. Changing accessor visibility

4.3. Compiler-generated methods: data classes and class delegation

4.3.1. Universal object methods

4.3.2. Data classes: autogenerated implementations of universal methods

4.3.3. Class delegation: using the "by" keyword

4.4. Declaring a class and creating an instance, combined, with the object keyword

4.4.1. Object declarations: singletons made easy

4.4.2. Companion objects: a place for factory methods and static members

4.4.3. Companion objects as regular objects

4.4.4. Object expressions: anonymous inner classes rephrased

4.5. Summary

5. Programming with lambdas

5.1. Lambda expressions and member references

5.1.1. Introduction to lambdas: blocks of code as method parameters

5.1.2. Lambdas and collections

5.1.3. Syntax for lambda expressions

5.1.4. Accessing variables in scope

5.1.5. Member references

5.2. Functional APIs for collections

5.2.1. Essentials: filter and map

5.2.2. all, any, count, and find: applying a predicate to a collection

5.2.3. groupBy: converting a list to a map of groups

5.2.4. flatMap and flatten: processing elements in nested collections

5.3. Lazy collection operations: sequences

5.3.1. Executing sequence operations: intermediate and terminal operations

5.3.2. Creating sequences

5.4. Using Java functional interfaces

5.4.1. Passing a lambda as a parameter to a Java method

5.4.2. SAM constructors: explicit conversion of lambdas to functional interfaces

5.5. Lambdas with receivers: with and apply

5.5.1. The "with" function

5.5.2. The "apply" function

5.6. Summary

6. The Kotlin type system

6.1. Nullability

6.1.1. Nullable types

6.1.2. The meaning of types

6.1.3. Safe call operator: "?."

6.1.4. Elvis operator: "?:"

6.1.5. Safe casts: "as?"

6.1.6. Not-null assertions: "!!"

6.1.7. The "let" function

6.1.8. Late-initialized properties

6.1.9. Extensions on nullable types

6.1.10. Nullability of type parameters

6.1.11. Nullability and Java

6.2. Primitive and other basic types

6.2.1. Primitive types: Int, Boolean, and more

6.2.2. Nullable primitive types: Int?, Boolean?, and more

6.2.3. Number conversions

6.2.4. "Any" and "Any?:" the root types

6.2.5. The Unit type: Kotlin's "void"

6.2.6. The Nothing type: "This function never returns"

6.3. Collections and arrays

6.3.1. Nullability and collections

6.3.2. Read-only and mutable collections

6.3.3. Kotlin collections and Java

6.3.4. Collections as platform types

6.3.5. Arrays of objects and primitive types

6.4. Summary

Part 2: Embracing Kotlin

7. Operator overloading and other conventions

7.1. Overloading arithmetic operators

7.1.1. Overloading binary arithmetic operations

7.1.2. Overloading compound assignment operators

7.1.3. Overloading unary operators

7.2. Overloading comparison operators

7.2.1. Equality operators: "equals"

7.2.2. Ordering operators: compareTo

7.3. Conventions used for collections and ranges

7.3.1. Accessing elements by index: "get" and "set"

7.3.2. The "in" convention

7.3.3. The rangeTo convention

7.3.4. The "iterator" convention for the "for" loop

7.4. Destructuring declarations and component functions

7.4.1. Destructuring declarations and loops

7.5. Reusing property accessor logic: delegated properties

7.5.1. Delegated properties: the basics

7.5.2. Using delegated properties: lazy initialization and by lazy()

7.5.3. Implementing delegated properties

7.5.4. Delegated-property translation rules

7.5.5. Storing property values in a map

7.5.6. Delegated properties in frameworks

7.6. Summary

8. Higher-order functions: lambdas as parameters and return values

8.1. Declaring higher-order functions

8.1.1. Function types

8.1.2. Calling functions passed as arguments

8.1.3. Using function types from Java

8.1.4. Default and null values for parameters with function types

8.1.5. Returning functions from functions

8.1.6. Removing duplication through lambdas

8.2. Inline functions: removing the overhead of lambdas

8.2.1. How inlining works

8.2.2. Restrictions on inline functions

8.2.3. Inlining collection operations

8.2.4. Deciding when to declare functions as inline

8.2.5. Using inlined lambdas for resource management

8.3. Control flow in higher-order functions

8.3.1. Return statements in lambdas: return from an enclosing function

8.3.2. Returning from lambdas: return with a label

8.3.3. Anonymous functions: local returns by default

8.4. Summary

9. Generics

9.1. Generic type parameters

9.1.1. Generic functions and properties

9.1.2. Declaring generic classes

9.1.3. Type parameter constraints

9.1.4. Making type parameters non-null

9.2. Generics at runtime: erased and reified type parameters

9.2.1. Generics at runtime: type checks and casts

9.2.2. Declaring functions with reified type parameters

9.2.3. Replacing class references with reified type parameters

9.2.4. Restrictions on reified type parameters

9.3. Variance: generics and subtyping

9.3.1. Why variance exists: passing an argument to a function

9.3.2. Classes, types, and subtyping

9.3.3. Covariance: preserved subtyping relation

9.3.4. Contravariance: reversed subtyping relation

9.3.5. Use-site variance: specifying variance for type occurrences

9.3.6. Star projection: using * instead of a type parameter

9.4. Summary

10. Annotations and Reflection

10.1. Declaring and applying annotations

10.1.1. Applying annotations

10.1.2. Annotation targets

10.1.3. Using annotations to customize JSON serialization

10.1.4. Declaring annotations

10.1.5. Meta-annotations: controlling how an annotation is processed

10.1.6. Classes as annotation parameters

10.1.7. Generic classes as annotation parameters

10.2. Reflection: Introspecting Kotlin objects at runtime

10.2.1. The Kotlin reflection API: KClass, KCallable, KFunction, KProperty

10.2.2. Implementing object serialization using reflection

10.2.3. Customizing serialization with annotations

10.2.4. JSON parsing and object deserialization

10.2.5. Final deserialization step: callBy() and creating objects using reflection

10.3. Summary

11. DSL construction

11.1. From APIs to DSLs

11.1.1. The concept of domain-specific languages

11.1.2. Internal DSLs

11.1.3. Structure of DSLs

11.1.4. Building HTML with an internal DSL

11.2. Building structured APIs: Lambdas with receiver in DSLs

11.2.1. Lambdas with receiver and extension function types

11.2.2. Using lambdas with receiver in HTML builders

11.2.3. Kotlin builders: Enabling abstraction and reuse

11.3. More flexible block nesting with the 'invoke' convention

11.3.1. The 'invoke' convention: Objects callable as functions

11.3.2. The 'invoke' convention and functional types

11.3.3. The 'invoke' convention in DSLs: Declaring dependencies in Gradle

11.4. Kotlin DSLs in practice

11.4.1. Chaining infix calls: 'should' in test frameworks

11.4.2. Defining extensions on primitive types: Date handling

11.4.3. Member extension functions: Internal DSL for SQL

11.4.4. Anko: Creating Android UIs dynamically

11.5. Summary


Appendix A: Building Kotlin projects

A.1. Building Kotlin code with Gradle

A.1.1. Building Kotlin Android applications with Gradle

A.1.2. Building projects that use annotation processing

A.2. Building Kotlin projects with Maven

A.3. Building Kotlin code with Ant

Appendix B: Documenting Kotlin code

B.1. Writing Kotlin documentation comments

B.2. Generating API documentation

Appendix C: The Kotlin ecosystem

C.1. Testing

C.2. Dependency injection

C.3. JSON serialization

C.4. HTTP clients

C.5. Web applications

C.6. Database access

C.7. Utilities and data structures

C.8. Desktop programming

About the Technology

Developers want to get work done - and the less hassle, the better. Coding with Kotlin means less hassle. The Kotlin programming language offers an expressive syntax, a strong intuitive type system, and great tooling support along with seamless interoperability with existing Java code, libraries, and frameworks. Kotlin can be compiled to Java bytecode, so you can use it everywhere Java is used, including Android. And with an effi cient compiler and a small standard library, Kotlin imposes virtually no runtime overhead.

About the book

Kotlin in Action teaches you to use the Kotlin language for production-quality applications. Written for experienced Java developers, this example-rich book goes further than most language books, covering interesting topics like building DSLs with natural language syntax. The authors are core Kotlin developers, so you can trust that even the gnarly details are dead accurate.

What's inside

  • Functional programming on the JVM
  • Writing clean and idiomatic code
  • Combining Kotlin and Java
  • Domain-specific languages

About the reader

This book is for experienced Java developers.

About the authors

Dmitry Jemerov and Svetlana Isakova are core Kotlin developers at JetBrains.

combo $44.99 pBook + eBook
eBook $35.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

Like all the other great in Action titles from Manning, this book gives you everything you need to become productive quickly.

Kevin Orr, Sumus Solutions

Kotlin is fun and easy to learn when you have this book to guide you!

Filip Pravica, Info.nl

Thorough, well written, and easily accessible.

Jason Lee, NetSuite