Java to Kotlin

Book description

It takes a week to travel the 8,000 miles overland from Java to Kotlin. If you're an experienced Java developer who has tried the Kotlin language, you were probably productive in about the same time.

You'll have found that they do things differently in Kotlin, though. Nullability is important, collections are different, and classes are final by default. Kotlin is more functional, but what does that mean, and how should it change the way that you program? And what about all that Java code that you still have to support?

Your tour guides Duncan and Nat first made the trip in 2015, and they've since helped many teams and individuals follow in their footsteps. Travel with them as they break the route down into legs like Optional to Nullable, Beans to Values, and Open to Sealed Classes. Each explains a key concept and then shows how to refactor production Java to idiomatic Kotlin, gradually and safely, while maintaining interoperability.

The resulting code is simpler, more expressive, and easier to change. By the end of the journey, you'll be confident in refactoring Java to Kotlin, writing Kotlin from scratch, and managing a mixed language codebase as it evolves over time.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. How This Book Is Organized
    2. How Did We Choose the Topics?
    3. Complexity
    4. Perfect Code
    5. Code Formatting
    6. Conventions Used in This Book
    7. Using Code Examples
    8. O’Reilly Online Learning
    9. How to Contact Us
    10. Acknowledgments
      1. Duncan’s Bit
      2. Nat’s Bit
  2. 1. Introduction
    1. The Grain of a Programming Language
    2. An Opinionated History of Java Programming Style
      1. Primeval Style
      2. Bean Style
      3. Enterprise Style
      4. Modern Style
      5. The Future
    3. The Grain of Kotlin
    4. Refactoring to Kotlin
      1. Refactoring Principles
      2. We Assume Good Test Coverage
      3. We Commit for Git Bisect
    5. What Are We Working On?
    6. Let’s Get Started!
  3. 2. Java to Kotlin Projects
    1. Strategy
    2. Adding Kotlin Support to a Java Build
    3. Moving On
  4. 3. Java to Kotlin Classes
    1. A Simple Value Type
    2. The Limitations of Data Classes
    3. Moving On
  5. 4. Optional to Nullable
    1. Representing Absence
    2. Refactoring from Optional to Nullable
    3. Refactoring to Idiomatic Kotlin
    4. Moving On
  6. 5. Beans to Values
    1. Beans
    2. Values
    3. Why Should We Prefer Values?
    4. Refactoring Beans to Values
    5. Moving On
  7. 6. Java to Kotlin Collections
    1. Java Collections
    2. Kotlin Collections
    3. Refactoring from Java to Kotlin Collections
      1. Fix Up the Java
      2. Convert to Kotlin
    4. Moving On
  8. 7. Actions to Calculations
    1. Functions
    2. Calculations
    3. Actions
    4. Why Should We Care?
    5. Why Prefer Calculations?
    6. Refactoring Actions to Calculations
      1. Existing Code
      2. A Better Design
      3. End Game
    7. Moving On
  9. 8. Static Methods to Top-Level Functions
    1. Java Statics
    2. Kotlin Top-Level Functions, Objects, and Companions
    3. Refactoring from Static Methods to Top-Level Functions
    4. Move to Top Level
    5. Kotlinify
    6. Moving On
  10. 9. Multi- to Single-Expression Functions
    1. Take 1: Inlining
    2. Take 2: Introduce a Function
    3. Take 3: Let
    4. Take 4: Stepping Back
    5. Moving On
  11. 10. Functions to Extension Functions
    1. Functions and Methods
    2. Extension Functions
    3. Extensions and Function Types
    4. Extension Properties
    5. Conversions
    6. Nullable Parameters
    7. Nullable Receivers
    8. Generics
    9. Extension Functions as Methods
    10. Refactoring to Extension Functions
    11. Moving On
  12. 11. Methods to Properties
    1. Fields, Accessors, and Properties
    2. How to Choose
    3. Mutable Properties
    4. Refactoring to Properties
    5. Moving On
  13. 12. Functions to Operators
    1. A Foundational Class: Money
    2. Adding a User-Defined Operator
    3. Calling Our Operator from Existing Kotlin Code
    4. Operators for Existing Java Classes
    5. Conventions for Denoting Values
    6. Moving On
  14. 13. Streams to Iterables to Sequences
    1. Java Streams
    2. Kotlin Iterables
    3. Kotlin Sequences
    4. Multiple Iterations
    5. Choosing Between Streams, Iterables, and Sequences
    6. Algebraic Transformation
    7. Refactoring from Streams to Iterables and Sequences
      1. Iterables First
      2. Then Sequences
      3. Extracting Part of a Pipeline
      4. Final Tidy
    8. Moving On
  15. 14. Accumulating Objects to Transformations
    1. Calculating with Accumulator Parameters
    2. Refactoring to Functions over Immutable Data
    3. Let’s Do That Again
    4. Enriching the Abstraction We Discovered
    5. Moving On
  16. 15. Encapsulated Collections to Type Aliases
    1. Composing Domain Collections
    2. Collections with Other Properties
    3. Refactoring Encapsulated Collections
      1. Convert Operations to Extensions
      2. Substitute a Type Alias
      3. Refactoring Collections with Other Properties
    4. Moving On
  17. 16. Interfaces to Functions
    1. Object-Oriented Encapsulation
    2. Functional Encapsulation
    3. Function Types in Java
    4. Mix and Match
    5. Comparing the Approaches
    6. Coupling
    7. Object-Oriented or Functional?
    8. The Legacy of Java
    9. Traceability
    10. Refactoring from Interfaces to Functions
      1. Introduce Functions
    11. Moving On
  18. 17. Mocks to Maps
    1. Replacing Mocks with Maps
    2. Have We Really Weaned Off Mocks, Though?
    3. Moving On
  19. 18. Open to Sealed Classes
    1. Polymorphism or Sealed Classes?
    2. Converting an Interface to a Sealed Class
    3. Moving On
  20. 19. Throwing to Returning
    1. Error Handling Before Exceptions
    2. Error Handling with Exceptions
    3. Java and Checked Exceptions
    4. Kotlin and Exceptions
    5. Beyond Exceptions: Functional Error Handling
    6. Error Handling in Kotlin
    7. Refactoring Exceptions to Errors
      1. Our Conversion Strategy
      2. Starting at the Bottom
      3. Contract
      4. Stepping Out
    8. More Fixup
    9. Layers
    10. Moving On
  21. 20. Performing I/O to Passing Data
    1. Listening to Tests
    2. I/O to Data
    3. Efficient Writing
    4. Efficient Reading
    5. Moving On
  22. 21. Exceptions to Values
    1. Identifying What Can Go Wrong
    2. Representing Errors
    3. What About I/O?
    4. Moving On
  23. 22. Classes to Functions
    1. An Acceptance Test
    2. Unit Testing
    3. Headers
    4. Different Field Separators
    5. Sequences
    6. Reading from a File
    7. Comparison with Commons CSV
    8. Moving On
  24. 23. Continuing the Journey
    1. Grain
    2. Functional Thinking
    3. Simple Design
    4. Functional Programming and Textual Reasoning
    5. Refactoring
    6. Refactoring and Functional Thinking
  25. Bibliography
    1. Learning Kotlin
    2. Testing and Test-Driven Development
    3. Working with Legacy Code
    4. Software Design and Development
    5. Java and the JVM
  26. Index

Product information

  • Title: Java to Kotlin
  • Author(s): Duncan McGregor, Nat Pryce
  • Release date: August 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492082279