Book LibraryTechnology & The FutureKotlin in Action (2nd Edition)
Book Cover

Kotlin in Action (2nd Edition)

by Sebastian Aigner
15.0 minutes

Kotlin in Action, Second Edition takes you from language basics to building production-quality applications that take advantage of Kotlin’s unique features. Discover how the language handles everything from statements and functions to classes and types, and the unique features that make Kotlin programming so seamless.In Kotlin in Action, Second Edition you will learn: Kotlin statements and functions, and classes and types Functional programming on the JVM The Kotlin standard library and out-of-the-box features Writing clean and idiomatic code Combining Kotlin and Java Improve code reliability with null safety Domain-specific languages Kotlin coroutines and flows Mastering the kotlinx.coroutines library Kotlin in Action, Second Edition is a complete guide to the Kotlin language written especially for readers familiar with Java or another OO language. Its authors—all core Kotlin language developers and Kotlin team members—share their unique insights, along with practical techniques and hands-on examples. This new second edition is fully updated to include the latest innovations, and it adds new chapters dedicated to coroutines, flows, and concurrency.Purchase of the print book includes a free eBook in PDF and ePub formats from Manning Publications.

Key Points

Kotlin in Action, Second Edition

This book provides a comprehensive guide to the Kotlin programming language, covering its core features, advanced concepts, and concurrent programming capabilities. It's aimed at Java developers who want to learn Kotlin and build applications on the JVM and Android.

Eager to master Kotlin and build stunning applications? Intrigued by Kotlin's unique approach to concurrency? Want to unlock the full potential of Kotlin in JVM and Android development? Then this book is your ultimate guide!

Core Content:

1. Kotlin's Key Traits:

  • Multi-paradigm: Combines object-oriented and functional programming for flexible and elegant code.

  • Statically Typed: Catches errors at compile time, improving performance and reliability.

  • Pragmatic: Integrates seamlessly with existing Java libraries and frameworks.

  • Coroutines: Offers a powerful way to write asynchronous code.

  • Multiplatform: Supports building cross-platform applications for desktop, iOS, Android, and browser.

    • Explanation: Kotlin's design balances modern language features with practical considerations, ensuring it fits well into existing development workflows.

2. Static Typing Benefits:

  • Performance: Faster method calls because types are resolved at compile time.

  • Reliability: Fewer runtime crashes due to compiler type verification.

  • Maintainability: Easier to understand unfamiliar code with clear type information.

  • Tool Support: Enables reliable refactorings and precise code completion in IDEs.

    • Explanation: Static typing enhances code quality and developer productivity by catching errors early.

3. Kotlin Philosophy:

  • Pragmatic: Addresses common Java pain points, such as verbosity and null safety.

  • Concise: Reduces boilerplate code, making programs easier to read and write.

  • Safe: Prevents common errors like NullPointerExceptions.

  • Interoperable: Fully compatible with Java, allowing mixed-language projects.

    • Explanation: Kotlin is designed to be both powerful and practical, providing a smooth transition for Java developers.

4. Basic Elements:

  • Data Classes: Automatically generate methods like

    toString()
    and
    equals()
    .

  • Read-Only Properties (val): Ensure immutability and data integrity.

  • Nullable Types (Int?): Explicitly handle potentially null values, preventing NullPointerExceptions.

  • Top-Level Functions: Define functions outside classes for utility operations.

  • Named Arguments: Improve code readability when calling functions.

  • String Templates: Simplify string formatting with embedded expressions.

    • Explanation: These features make Kotlin code more expressive and easier to maintain.

5. Functions:

  • Extension Functions: Add methods to existing classes without modifying their source code.

  • Higher-Order Functions: Accept functions as arguments and/or return them, enabling powerful abstractions.

    • Explanation: Functional programming concepts enhance code reusability and flexibility.

6. Concurrent Programming:

  • Coroutines: Lightweight threads that simplify asynchronous code.

  • Flows: Represent asynchronous streams of data.

    • Explanation: Kotlin provides modern tools for handling concurrency, making asynchronous programming easier and more efficient.

Q&A:

Q: What are the key benefits of using Kotlin?

A: Kotlin offers improved conciseness, safety, and interoperability compared to Java. Its modern features and strong tool support enhance developer productivity and code quality.

Q: How does Kotlin prevent NullPointerExceptions?

A: Kotlin uses nullable types (e.g.,

String?
) to explicitly mark variables that can be null. This forces developers to handle null cases, preventing unexpected runtime errors.

Q: What is Kotlin Multiplatform, and why is it useful?

A: Kotlin Multiplatform allows you to share code between different platforms (e.g., Android, iOS, desktop). It reduces duplication and simplifies cross-platform development.

Q: Can I use Java libraries in Kotlin projects?

A: Yes, Kotlin is fully interoperable with Java. You can use Java libraries directly in Kotlin code and vice versa.

Q: What is the recommended build system for Kotlin projects?

A: Gradle is the recommended build system for Kotlin. It offers flexible project models and excellent build performance.

MindMap

Target Audience

Primarily focused on developers with some level of Java experience. Also suitable for developers experienced with other programming languages such as C# or JavaScript.

Author Background

Sebastian Aigner is a developer advocate at JetBrains, a host on the Talking Kotlin podcast, and a member of the Kotlin Foundation.

Historical Context

The idea for Kotlin was conceived at JetBrains in 2010, born out of the need for a modern, powerful, and rapidly evolving language to replace Java for internal development. Kotlin 1.0 was released more than five years after the first commit to the repository.

Chapter Summary

Audio

Coming Soon...