Programming Kotlin
Create Elegant, Expressive, and Performant JVM and Android Applications
by Venkat Subramaniam
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 maintainable,
high-performing JVM and Android applications, create DSLs, program
asynchronously, 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 stand-alone
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 asynchronously, 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.
Resources
Releases:
- P1.0 2019/09/22
- B12.0 2019/08/26
- B11.0 2019/06/20
- B10.0 2019/05/16
- Introduction
- Hello Kotlin
excerpt
- Reasons to Love Kotlin
- Why Should You Choose Kotlin?
- Taking Kotlin for a Ride
- Compile to Bytecode and Run
- Compiling to Other Targets
- Which Option to Choose?
- Wrapping Up
- Scripting with Kotlin
- Kotlin Essentials for the Java Eyes
- Less Typing
- Sensible Warnings
excerpt
- Prefer val over var
- Improved Equality Check
- String Templates
- Raw Strings
- More Expressions, Fewer Statements
- Wrapping Up
- Working with Functions
- Creating Functions
- Default and Named Arguments
- vararg and Spread
- Destructuring
- Wrapping Up
- External Iteration and Argument Matching
- Range and Iteration
- Iterating over Arrays and Lists
- When It’s Time to Use when
- Wrapping Up
- Using Collections
- Flavors of Collections
- Using Pair and Triple
- Arrays of Objects and Primitives
- Using List
- Using Set
- Using Map
- Wrapping Up
- Type Safety to Save the Day
- Any and Nothing Classes
- Nullable References
- Type Checking and Casting
- Explicit Type Casting
- Generics: Variance and Constraints of Parametric Types
- Reified Type Parameters
- Wrapping Up
- Object-Oriented Kotlin
- Objects and Classes
- Objects and Singletons
- Creating Classes
excerpt
- Companion Objects and Class Members
- Creating Generics Classes
- Data Classes
- Wrapping Up
- Class Hierarchies and Inheritance
- Creating Interfaces and Abstract Classes
- Nested and Inner Classes
- Inheritance
- Sealed Classes
- Creating and Using Enums
- Wrapping Up
- Extension Through Delegation
- When to Choose Delegation over Inheritance?
- Designing with Delegates
- Delegating to a Parameter
- Dealing with Method Collisions
- Caveats of Kotlin Delegation
- Delegating Variables and Properties
- Built-in Standard Delegates
- Wrapping Up
- Functional Kotlin
- Functional Programming with Lambdas
- The Functional Style
- Lambda Expressions
- Lambdas and Anonymous Functions
- Closures and Lexical Scoping
- Non-Local and Labeled return
- Inlining Functions with Lambdas
- Wrapping Up
- Internal Iteration and Lazy Evaluation
- External vs. Internal Iterators
- Internal Iterators
- Sequences for Lazy Evaluation
- Wrapping Up
- Elegant and Efficient Kotlin
- Fluency in Kotlin
- Overloading Operators
- Injecting Using Extension Functions and Properties
- Extending Functions
- Function Fluency with infix
- Fluency with Any Object
- Implicit Receivers
- Wrapping Up
- Creating Internal DSLs
- Types and Characteristics of DSLs
- Kotlin for Internal DSLs
- Challenges in Building for Fluency
- Type-Safe Builders
- Narrowing Access with Scope Control
- Wrapping Up
- Programming Recursion and Memoization
- The Power and Perils of Recursion
- Tail Call Optimization
- Memoization
- Applying Memoization to Dynamic Programming
- Wrapping Up
- Programming Asynchronous Applications
- Exploring Coroutines
- Coroutines and Concurrency
- Running Concurrently Using Coroutines
- Coroutine Context and Threads
- Debugging Coroutines
- async and await
- A Peek at Continuations
- Creating Infinite Sequences
- Wrapping Up
- Asynchronous Programming
- Programming Asynchronously
- Exception Handling
- Cancellations and Timeouts
- Wrapping Up
- Interop and Testing
- Intermixing Java and Kotlin
- Joint Compilation
- Calling Java from Kotlin
- Calling Kotlin from Java
- Wrapping Up
- Unit Testing with Kotlin
- The Code Under Test
- Getting the Project Files
- Starting with a Canary Test
- Writing Empirical Tests
- Writing Data-Driven Tests
- Mocking Out Dependencies
- Testing Top-Level Functions
- Testing Coroutines and Asynchronous Calls
- Integrating with the Service
- Viewing the Code Coverage
- Taking the App for a Drive
- Wrapping Up
- Programming Spring Applications with Kotlin
- Creating a Starter Project
- Creating a Controller
- Creating an Entity Class
- Creating a Repository Interface
- Creating a Service
- Integrating the Service with Controller
- Taking It for a Ride
- Wrapping Up
- Writing Android Applications with Kotlin
- Creating a Project
- Defining Domain Objects
- Creating Layouts
- Implementing the Activity
- Updating the RecyclerView
- Seeing the App in Action
- Wrapping Up
Author
Dr. Venkat Subramaniam is an award-winning author, founder of Agile
Developer, Inc., and an instructional professor at the University of
Houston. He has trained and mentored thousands of software developers in
the US, Canada, Europe, and Asia, and is regularly invited to speak at
international conferences. He’s the (co)author of multiple books,
including the 2007 Jolt Productivity award winning book Practices of an
Agile Developer.