Programming Kotlin

Book description

Programmers don't just use Kotlin, they love it. Even Google has adopted it as a first-class language for Android development. With Kotlin, you can intermix imperative, functional, and object-oriented styles of programming and benefit from the approach that's most suitable for the problem at hand. Learn to use the many features of this highly concise, fluent, elegant, and expressive statically typed language with easy-to-understand examples. Learn to write easy-to-maintain, high-performing JVM and Android applications, create DSLs, program asynchrony, and much more.

Kotlin is a highly concise, elegant, fluent, and expressive statically typed multi-paradigm language. It is one of the few languages that compiles down to both Java bytecode and JavaScript. You can use it to build server-side, front-end, and Android applications. With Kotlin, you need less code to accomplish your tasks, while keeping the code type-safe and less prone to error. If you want to learn the essentials of Kotlin, from the fundamentals to more advanced concepts, you've picked the right book.

Fire up your favorite IDE and practice hundreds of examples and exercises to sharpen your Kotlin skills. Learn to build standalone small programs to run as scripts, create type safe code, and then carry that knowledge forward to create fully object-oriented and functional style code that's easier to extend. Learn how to program with elegance but without compromising efficiency or performance, and how to use metaprogramming to build highly expressive code and create internal DSLs that exploit the fluency of the language. Explore coroutines, program asynchrony, run automated tests, and intermix Kotlin with Java in your enterprise applications.

This book will help you master one of the few languages that you can use for the entire full stack - from the server to mobile devices - to create performant, concise, and easy to maintain applications.

What You Need:

To try out the examples in the book you'll need a computer with Kotlin SDK, JDK, and a text editor or a Kotlin IDE installed in it.

Publisher resources

View/Submit Errata

Table of contents

  1.  Foreword
  2.  Acknowledgments
  3.  Introduction
    1. Who Is This Book For?
    2. What’s in This Book?
    3. Kotlin and Java Versions Used in This Book
    4. How to Read the Code Examples
    5. Online Resources
  4. 1. Hello Kotlin
    1. Reasons to Love Kotlin
    2. Why Should You Choose Kotlin?
    3. Taking Kotlin for a Ride
    4. Compile to Bytecode and Run
    5. Compiling to Other Targets
    6. Which Option to Choose?
    7. Wrapping Up
  5. Part I. Scripting with Kotlin
    1. 2. Kotlin Essentials for the Java Eyes
      1. Less Typing
      2. Sensible Warnings
      3. Prefer val over var
      4. Improved Equality Check
      5. String Templates
      6. Raw Strings
      7. More Expressions, Fewer Statements
      8. Wrapping Up
    2. 3. Working with Functions
      1. Creating Functions
      2. Default and Named Arguments
      3. vararg and Spread
      4. Destructuring
      5. Wrapping Up
    3. 4. External Iteration and Argument Matching
      1. Range and Iteration
      2. Iterating over Arrays and Lists
      3. When It’s Time to Use when
      4. Wrapping Up
    4. 5. Using Collections
      1. Flavors of Collections
      2. Using Pair and Triple
      3. Arrays of Objects and Primitives
      4. Using List
      5. Using Set
      6. Using Map
      7. Wrapping Up
    5. 6. Type Safety to Save the Day
      1. Any and Nothing Classes
      2. Nullable References
      3. Type Checking and Casting
      4. Explicit Type Casting
      5. Generics: Variance and Constraints of Parametric Types
      6. Reified Type Parameters
      7. Wrapping Up
  6. Part II. Object-Oriented Kotlin
    1. 7. Objects and Classes
      1. Objects and Singletons
      2. Creating Classes
      3. Companion Objects and Class Members
      4. Creating Generics Classes
      5. Data Classes
      6. Wrapping Up
    2. 8. Class Hierarchies and Inheritance
      1. Creating Interfaces and Abstract Classes
      2. Nested and Inner Classes
      3. Inheritance
      4. Sealed Classes
      5. Creating and Using Enums
      6. Wrapping Up
    3. 9. Extension Through Delegation
      1. When to Choose Delegation over Inheritance?
      2. Designing with Delegates
      3. Delegating to a Parameter
      4. Dealing with Method Collisions
      5. Caveats of Kotlin Delegation
      6. Delegating Variables and Properties
      7. Built-in Standard Delegates
      8. Wrapping Up
  7. Part III. Functional Kotlin
    1. 10. Functional Programming with Lambdas
      1. The Functional Style
      2. Lambda Expressions
      3. Lambdas and Anonymous Functions
      4. Closures and Lexical Scoping
      5. Non-Local and Labeled return
      6. Inlining Functions with Lambdas
      7. Wrapping Up
    2. 11. Internal Iteration and Lazy Evaluation
      1. External vs. Internal Iterators
      2. Internal Iterators
      3. Sequences for Lazy Evaluation
      4. Wrapping Up
  8. Part IV. Elegant and Efficient Kotlin
    1. 12. Fluency in Kotlin
      1. Overloading Operators
      2. Injecting Using Extension Functions and Properties
      3. Extending Functions
      4. Function Fluency with infix
      5. Fluency with Any Object
      6. Implicit Receivers
      7. Wrapping Up
    2. 13. Creating Internal DSLs
      1. Types and Characteristics of DSLs
      2. Kotlin for Internal DSLs
      3. Challenges in Building for Fluency
      4. Type-Safe Builders
      5. Narrowing Access with Scope Control
      6. Wrapping Up
    3. 14. Programming Recursion and Memoization
      1. The Power and Perils of Recursion
      2. Tail Call Optimization
      3. Memoization
      4. Applying Memoization to Dynamic Programming
      5. Wrapping Up
  9. Part V. Programming Asynchronous Applications
    1. 15. Exploring Coroutines
      1. Coroutines and Concurrency
      2. Running Concurrently Using Coroutines
      3. Coroutine Context and Threads
      4. Debugging Coroutines
      5. async and await
      6. A Peek at Continuations
      7. Creating Infinite Sequences
      8. Wrapping Up
    2. 16. Asynchronous Programming
      1. Programming Asynchronously
      2. Exception Handling
      3. Cancellations and Timeouts
      4. Wrapping Up
  10. Part VI. Interop and Testing
    1. 17. Intermixing Java and Kotlin
      1. Joint Compilation
      2. Calling Java from Kotlin
      3. Calling Kotlin from Java
      4. Wrapping Up
    2. 18. Unit Testing with Kotlin
      1. The Code Under Test
      2. Getting the Project Files
      3. Starting with a Canary Test
      4. Writing Empirical Tests
      5. Writing Data-Driven Tests
      6. Mocking Out Dependencies
      7. Testing Top-Level Functions
      8. Testing Coroutines and Asynchronous Calls
      9. Integrating with the Service
      10. Viewing the Code Coverage
      11. Taking the App for a Drive
      12. Wrapping Up
    3. 19. Programming Spring Applications with Kotlin
      1. Creating a Starter Project
      2. Creating a Controller
      3. Creating an Entity Class
      4. Creating a Repository Interface
      5. Creating a Service
      6. Integrating the Service with Controller
      7. Taking It for a Ride
      8. Wrapping Up
    4. 20. Writing Android Applications with Kotlin
      1. Creating a Project
      2. Defining Domain Objects
      3. Creating Layouts
      4. Implementing the Activity
      5. Updating the RecyclerView
      6. Seeing the App in Action
      7. Wrapping Up
  11. A1. Transpiling to JavaScript
  12. A2. Kotlin/Native
  13. A3. Kotlin to WebAssembly
  14.  Bibliography

Product information

  • Title: Programming Kotlin
  • Author(s): Venkat Subramaniam
  • Release date: September 2019
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680506358