Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition

Book description

Kotlin is a statically typed programming language designed to interoperate with Java and fully supported by Google on the Android operating system.

Based on Big Nerd Ranchs popular Kotlin Essentials course, and written for Kotlin 1.4, this guide shows you how to work effectively with the Kotlin programming language through hands-on examples and clear explanations of key Kotlin concepts and foundational APIs.

Whether you are an experienced Android developer looking for modern features beyond what Java offers or a new developer ready to learn your first programming language, the authors will guide you from first principles to advanced usage of Kotlin. By the end of this book, you will be empowered to create reliable, concise applications in Kotlin.

Table of contents

  1. Title Page
  2. Dedication
  3. Acknowledgments
  4. Table of Contents
  5. Introducing Kotlin
    1. Why Kotlin?
    2. Who Is This Book For?
    3. How to Use This Book
      1. For the More Curious
      2. Challenges
      3. Typographical conventions
      4. Using an eBook
    4. Looking Forward
  6. I. Getting Started
    1. 1. Your First Kotlin Application
      1. Installing IntelliJ IDEA
      2. Your First Kotlin Project
        1. Creating your first Kotlin file
        2. Running your Kotlin file
          1. Compilation and execution of Kotlin/JVM code
      3. The Kotlin REPL
      4. For the More Curious: Why Use IntelliJ?
      5. For the More Curious: Targeting the JVM
      6. Challenge: REPL Arithmetic
    2. 2. Variables, Constants, and Types
      1. Types
      2. Declaring a Variable
      3. Kotlin’s Built-In Types
      4. Read-Only Variables
      5. Type Inference
      6. Compile-Time Constants
      7. Inspecting Kotlin Bytecode
      8. For the More Curious: Java Primitive Types in Kotlin
      9. Challenge: hasSteed
      10. Challenge: The Unicorn’s Horn
      11. Challenge: Magic Mirror
  7. II. Basic Syntax
    1. 3. Conditionals
      1. if/else Statements
        1. Adding more conditions
        2. Nested if/else statements
        3. More elegant conditionals
          1. Logical operators
          2. Conditional expressions
          3. Removing braces from if/else expressions
      2. Ranges
      3. when Expressions
        1. when expressions with variable declarations
        2. when expressions without arguments
      4. Challenge: Trying Out Some Ranges
    2. 4. Functions
      1. Extracting Code to Functions
      2. Anatomy of a Function
        1. Function header
          1. Visibility modifier
          2. Function name declaration
          3. Function parameters
          4. Function return type
        2. Function body
        3. Function scope
      3. Calling a Function
      4. Writing Your Own Functions
      5. Default Arguments
      6. Single-Expression Functions
      7. Unit Functions
      8. Named Function Arguments
      9. For the More Curious: The Nothing Type
      10. For the More Curious: File-Level Functions in Java
      11. For the More Curious: Function Overloading
      12. For the More Curious: Function Names in Backticks
    3. 5. Numbers
      1. Numeric Types
      2. Integers
      3. Floating Point Numbers
      4. Formatting a Double
      5. Converting Between Numeric Types
      6. For the More Curious: Unsigned Numbers
      7. For the More Curious: Bit Manipulation
    4. 6. Strings
      1. String Interpolation
      2. Raw Strings
      3. Reading Console Input
      4. Converting Strings to Numbers
      5. Regular Expressions
      6. String Manipulation
        1. Strings are immutable
      7. String Comparison
      8. For the More Curious: Unicode
    5. 7. Null Safety and Exceptions
      1. Nullability
      2. Kotlin’s Explicit Null Type
      3. Compile Time vs Runtime
      4. Null Safety
        1. Option one: checking for null values with an if statement
        2. Option two: the safe call operator
          1. Option two and a half: using safe calls with let
          2. The null coalescing operator
        3. Option three: the non-null assertion operator
      5. Exceptions
        1. Throwing an exception
        2. Handling exceptions
        3. Try/catch expressions
      6. Preconditions
      7. For the More Curious: Custom Exceptions
      8. For the More Curious: Checked vs Unchecked Exceptions
  8. III. Introduction to Functional Programming and Collections
    1. 8. Lambda Expressions and the Function Type
      1. Introducing NyetHack
      2. Anonymous Functions
      3. Lambda Expressions
        1. The function type
        2. Implicit returns
        3. Function arguments
        4. The it identifier
        5. Accepting multiple arguments
      4. Type Inference Support
      5. More Effective Lambdas
      6. Defining a Function That Accepts a Function
        1. Trailing lambda syntax
      7. Function Inlining
      8. Lambdas and the Kotlin Standard Library
      9. For the More Curious: Function References
      10. For the More Curious: Capturing Lambdas
      11. Challenge: New Titles and Moods
    2. 9. Lists and Sets
      1. Lists
        1. Accessing a list’s elements
          1. Index boundaries and safe index access
          2. Checking the contents of a list
        2. Changing a list’s contents
      2. Iteration
      3. Reading a File into a List
      4. Destructuring
      5. Sets
        1. Creating a set
        2. Adding elements to a set
      6. while Loops
      7. Collection Conversion
      8. For the More Curious: Array Types
      9. For the More Curious: Read-Only vs Immutable
      10. For the More Curious: The break Expression
      11. For the More Curious: Return Labels
      12. Challenge: Formatted Tavern Menu
      13. Challenge: Advanced Formatted Tavern Menu
    3. 10. Maps
      1. Creating a Map
      2. Accessing Map Values
      3. Adding Entries to a Map
      4. Modifying Map Values
      5. Converting Between Lists and Maps
      6. Iterating Through a Map
      7. Challenge: Complex Orders
    4. 11. Functional Programming Basics
      1. Transforming Data
        1. map
        2. associate
        3. Destructuring with functional programming
        4. flatMap
          1. map vs flatMap
      2. Filtering Data
        1. filter
      3. Combining Data
        1. zip
      4. Why Functional Programming?
      5. Sequences
      6. For the More Curious: Profiling
      7. For the More Curious: Aggregating Data
        1. reduce
        2. fold
        3. sumBy
      8. For the More Curious: The vararg Keyword
      9. For the More Curious: Arrow.kt
      10. Challenge: Reversing the Values in a Map
      11. Challenge: Finding the Most-Liked Menu Item
    5. 12. Scope Functions
      1. apply
      2. let
      3. run
      4. with
      5. also
      6. takeIf
      7. Using Scope Functions
  9. IV. Object-Oriented Programming
    1. 13. Classes
      1. Defining a Class
      2. Constructing Instances
      3. Class Functions
      4. Visibility and Encapsulation
      5. Class Properties
        1. Property getters and setters
        2. Property visibility
        3. Computed properties
      6. Using Packages
      7. For the More Curious: A Closer Look at var and val Properties
      8. For the More Curious: Guarding Against Mutability
      9. For the More Curious: Package Private
    2. 14. Initialization
      1. Constructors
        1. Primary constructors
        2. Defining properties in a primary constructor
        3. Secondary constructors
        4. Default arguments
        5. Named arguments
      2. Initializer Blocks
      3. Initialization Order
      4. Delaying Initialization
        1. Late initialization
        2. Lazy initialization
      5. For the More Curious: Initialization Gotchas
      6. For the More Curious: Property Delegates
      7. Challenge: The Riddle of Excalibur
    3. 15. Inheritance
      1. Defining the Room Class
      2. Creating a Subclass
      3. Type Checking
      4. The Kotlin Type Hierarchy
        1. Type casting
        2. Smart casting
      5. Refactoring the Tavern
      6. For the More Curious: Any
      7. For the More Curious: The Safe Cast Operator
    4. 16. Objects, Data Classes, and Enums
      1. The object Keyword
        1. Object declarations
        2. Object expressions
        3. Companion objects
      2. Nested Classes
      3. Data Classes
        1. toString
        2. equals and hashCode
        3. copy
        4. Destructuring declarations
      4. Enumerated Classes
      5. Operator Overloading
      6. Exploring the World of NyetHack
      7. For the More Curious: Defining Structural Comparison
      8. For the More Curious: Algebraic Data Types
      9. For the More Curious: Value Classes
      10. Challenge: More Commands
      11. Challenge: Implementing a World Map
      12. Challenge: Ring the Bell
    5. 17. Interfaces and Abstract Classes
      1. Defining an Interface
      2. Implementing an Interface
      3. Default Implementations
      4. Abstract Classes
      5. Combat in NyetHack
      6. Challenge: Additional Monsters
  10. V. Advanced Kotlin
    1. 18. Generics
      1. Defining Generic Types
      2. Generic Functions
      3. Generic Constraints
      4. in and out
      5. Adding Loot to NyetHack
      6. For the More Curious: The reified Keyword
    2. 19. Extensions
      1. Defining Extension Functions
        1. Defining an extension on a superclass
        2. Generic extension functions
        3. Operator extension functions
      2. Extension Properties
      3. Extensions on Nullable Types
      4. Extensions, Under the Hood
      5. Extension Visibility
      6. Extensions in the Kotlin Standard Library
      7. For the More Curious: Function Literals with Receivers
      8. Challenge: Frame Extension
    3. 20. Coroutines
      1. Blocking Calls
      2. Enabling Coroutines
      3. Coroutine Builders
      4. Coroutine Scopes
      5. Structured Concurrency
      6. Using an HTTP Client
      7. async and await
      8. For the More Curious: Race Conditions
      9. For the More Curious: Server-Side Kotlin
      10. Challenge: No Cancellations
    4. 21. Flows
      1. Setting Up a Flow
      2. MutableStateFlow
      3. Flow Termination
      4. Flow Transformations
      5. Error Handling in Flows
      6. For the More Curious: SharedFlow
    5. 22. Channels
      1. Dividing Work with Channels
      2. Sending to a Channel
      3. Receiving from a Channel
      4. Closing a Channel
      5. Joining Jobs
      6. For the More Curious: Other Channel Behaviors
        1. Rendezvous channels
        2. Buffered channels
        3. Unlimited channels
        4. Conflated channels
  11. VI. Interoperation and Multiplatform Applications
    1. 23. Java Interoperability
      1. Interoperating with a Java Class
      2. Interoperability and Nullity
      3. Type Mapping
      4. Getters, Setters, and Interoperability
      5. Beyond Classes
      6. Exceptions and Interoperability
      7. Function Types in Java
    2. 24. Introduction to Kotlin Multiplatform
      1. What Is Kotlin Multiplatform?
      2. Planning Your Multiplatform Project
      3. Your First Multiplatform Project
      4. Defining a Kotlin/JVM Target
      5. Defining Shared Code
      6. expect and actual
    3. 25. Kotlin/Native
      1. Declaring a macOS Target
      2. Writing Native Code with Kotlin
      3. Launching a Kotlin/Native application
      4. Kotlin/Native Outputs
      5. For the More Curious: Kotlin Multiplatform Mobile
      6. For the More Curious: Other Native Platforms
    4. 26. Kotlin/JS
      1. Declaring Support for Kotlin/JS
      2. Interacting with the DOM
      3. The external Keyword
      4. Executing Raw JavaScript
      5. Dynamic Types
      6. For the More Curious: Front-End Frameworks
      7. Challenge: Currency Exchange Fees
  12. 27. Afterword
    1. Where to Go from Here
    2. Shameless Plugs
    3. Thank You
  13. Glossary
  14. Index

Product information

  • Title: Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition
  • Author(s): David Greenhalgh, Josh Skeen
  • Release date: October 2021
  • Publisher(s): Big Nerd Ranch Guides
  • ISBN: 9780136870494