Kotlin Programming: The Big Nerd Ranch Guide, First 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 Ranch's popular Kotlin Essentials course, 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. Written for Kotlin 1.2, this book will also introduce you to JetBrains' IntelliJ IDEA development environment.

 

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. Cover Page
  2. Title Page
  3. Dedication
  4. Acknowledgments
  5. Table of Contents
  6. 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
  7. 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
  8. 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
  9. 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
    4. String Templates
    5. Challenge: Trying Out Some Ranges
    6. Challenge: Enhancing the Aura
    7. Challenge: Configurable Status Format
  10. 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. Refactoring to Functions
    5. Writing Your Own Functions
    6. Default Arguments
    7. Single-Expression Functions
    8. Unit Functions
    9. Named Function Arguments
    10. For the More Curious: The Nothing Type
    11. For the More Curious: File-Level Functions in Java
    12. For the More Curious: Function Overloading
    13. For the More Curious: Function Names in Backticks
    14. Challenge: Single-Expression Functions
    15. Challenge: Fireball Inebriation Level
    16. Challenge: Inebriation Status
  11. 5. Anonymous Functions and the Function Type
    1. Anonymous Functions
      1. The function type
      2. Implicit returns
      3. Function arguments
      4. The it keyword
      5. Accepting multiple arguments
    2. Type Inference Support
    3. Defining a Function That Accepts a Function
      1. Shorthand syntax
    4. Function Inlining
    5. Function References
    6. Function Type as Return Type
    7. For the More Curious: Kotlin’s Lambdas Are Closures
    8. For the More Curious: Lambdas vs Anonymous Inner Classes
  12. 6. Null Safety and Exceptions
    1. Nullability
    2. Kotlin’s Explicit Null Type
    3. Compile Time vs Runtime
    4. Null Safety
      1. Option one: the safe call operator
        1. Using safe calls with let
      2. Option two: the double-bang operator
      3. Option three: checking whether a value is null with if
        1. The null coalescing operator
    5. Exceptions
      1. Throwing an exception
      2. Custom exceptions
      3. Handling exceptions
    6. Preconditions
    7. Null: What Is It Good For?
    8. For the More Curious: Checked vs Unchecked Exceptions
    9. For the More Curious: How Is Nullability Enforced?
  13. 7. Strings
    1. Extracting Substrings
      1. substring
      2. split
    2. String Manipulation
      1. Strings are immutable
    3. String Comparison
    4. For the More Curious: Unicode
    5. For the More Curious: Traversing a String’s Characters
    6. Challenge: Improving DragonSpeak
  14. 8. Numbers
    1. Numeric Types
    2. Integers
    3. Decimal Numbers
    4. Converting a String to a Numeric Type
    5. Converting an Int to a Double
    6. Formatting a Double
    7. Converting a Double to an Int
    8. For the More Curious: Bit Manipulation
    9. Challenge: Remaining Pints
    10. Challenge: Handling a Negative Balance
    11. Challenge: Dragoncoin
  15. 9. Standard Functions
    1. apply
    2. let
    3. run
    4. with
    5. also
    6. takeIf
      1. takeUnless
    7. Using Standard Library Functions
  16. 10. 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. The break Expression
    8. Collection Conversion
    9. For the More Curious: Array Types
    10. For the More Curious: Read-Only vs Immutable
    11. Challenge: Formatted Tavern Menu
    12. Challenge: Advanced Formatted Tavern Menu
  17. 11. Maps
    1. Creating a Map
    2. Accessing Map Values
    3. Adding Entries to a Map
    4. Modifying Map Values
    5. Challenge: Tavern Bouncer
  18. 12. Defining 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. Refactoring NyetHack
    7. Using Packages
    8. For the More Curious: A Closer Look at var and val Properties
    9. For the More Curious: Guarding Against Race Conditions
    10. For the More Curious: Package Private
  19. 13. 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. Property Initialization
    4. Initialization Order
    5. Delaying Initialization
      1. Late initialization
      2. Lazy initialization
    6. For the More Curious: Initialization Gotchas
    7. Challenge: The Riddle of Excalibur
  20. 14. 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. For the More Curious: Any
  21. 15. Objects
    1. The object Keyword
      1. Object declarations
      2. Object expressions
      3. Companion objects
    2. Nested Classes
    3. Data Classes
      1. toString
      2. equals
      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. Challenge: “Quit” Command
    10. Challenge: Implementing a World Map
    11. Challenge: Ring the Bell
  22. 16. Interfaces and Abstract Classes
    1. Defining an Interface
    2. Implementing an Interface
    3. Default Implementations
    4. Abstract Classes
    5. Combat in NyetHack
  23. 17. Generics
    1. Defining Generic Types
    2. Generic Functions
    3. Multiple Generic Type Parameters
    4. Generic Constraints
    5. vararg and get
    6. in and out
    7. For the More Curious: The reified Keyword
  24. 18. Extensions
    1. Defining Extension Functions
      1. Defining an extension on a superclass
    2. Generic Extension Functions
    3. Extension Properties
    4. Extensions on Nullable Types
    5. Extensions, Under the Hood
    6. Extracting to Extensions
    7. Defining an Extensions File
    8. Renaming an Extension
    9. Extensions in the Kotlin Standard Library
    10. For the More Curious: Function Literals with Receivers
    11. Challenge: toDragonSpeak Extension
    12. Challenge: Frame Extension
  25. 19. Functional Programming Basics
    1. Function Categories
      1. Transforms
      2. Filters
      3. Combines
    2. Why Functional Programming?
    3. Sequences
    4. For the More Curious: Profiling
    5. For the More Curious: Arrow.kt
    6. Challenge: Reversing the Values in a Map
    7. Challenge: Applying Functional Programming to Tavern.kt
    8. Challenge: Sliding Window
  26. 20. 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
  27. 21. Building Your First Android Application with Kotlin
    1. Android Studio
      1. Gradle configuration
      2. Project organization
    2. Defining a UI
    3. Running the App on an Emulator
    4. Generating a Character
    5. The Activity Class
    6. Wiring Up Views
    7. Kotlin Android Extensions Synthetic Properties
    8. Setting a Click Listener
    9. Saved Instance State
      1. Reading from the saved instance state
    10. Refactoring to an Extension
    11. For the More Curious: Android KTX and Anko Libraries
  28. 22. Introduction to Coroutines
    1. Parsing Character Data
    2. Fetching Live Data
    3. The Android Main Thread
    4. Enabling Coroutines
    5. Specifying a Coroutine with async
    6. launch vs async/await
    7. Suspending Functions
    8. Challenge: Live Data
    9. Challenge: Minimum Strength
  29. 23. Afterword
    1. Where to Go from Here
    2. Shameless Plugs
    3. Thank You
  30. A. More Challenges
    1. Leveling Up with Exercism
  31. Glossary
  32. Index

Product information

  • Title: Kotlin Programming: The Big Nerd Ranch Guide, First Edition
  • Author(s): Josh Skeen, David Greenhalgh
  • Release date: July 2018
  • Publisher(s): Big Nerd Ranch Guides
  • ISBN: 9780135165188