Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorDuncan McGregor, Nat Pryce

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.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical field guide for experienced Java developers who want to move to Kotlin without rewriting everything at once. It shows how to refactor real production code, leg by leg, into idiomatic Kotlin while keeping Java and Kotlin interoperating in the same codebase. 【Book Arc】 - **Opening (~0%–10%)**: Sets up the journey and the mindset. It frames the Java-to-Kotlin move as a gradual, safe refactoring rather than a rewrite, and explains why the two languages differ in ways that matter (nullability, collections, final-by-default classes). - **Early (~10%–30%)**: Establishes the migration strategy and core value-model changes. Covers introducing Kotlin into a Java project, converting beans to immutable values, and replacing Java's `Optional` with Kotlin's first-class nullable types. - **Middle (~30%–60%)**: Works through the language-grain differences in depth. Collections, static methods to top-level functions, multi-expression to single-expression functions, functions to extension functions, and methods to properties. - **Late (~60%–90%)**: Moves into more advanced refactorings and design shifts. Streams to iterables to sequences, encapsulated collections to type aliases, and open to sealed classes, plus handling I/O and passing data. - **Ending (~90%–100%)**: Consolidates the journey. The reader should finish confident refactoring Java to Kotlin, writing Kotlin from scratch, and managing a mixed-language codebase as it evolves. 【Key Takeaways】 - **Migration is a journey, not a rewrite** (Early): The book's central premise is incremental, safe refactoring that preserves interoperability, so you can convert a codebase piece by piece without a big-bang cutover. - **Nullability is a type-system feature, not a library** (Early): Kotlin embraces null in the type system, which gives codebases uniformity in handling absence; the authors advise against wrapping optionality in an `Optional`-style type because you lose Kotlin's dedicated language features. - **Prefer immutable values over mutable beans** (Early/Middle): JavaBeans were designed for mutable GUI builders, but mutability brings complications; converting beans to values simplifies code and makes it easier to change. - **Kotlin's collections differ from Java's in important ways** (Middle): The distinction between `List` and `MutableList` is not a guarantee of immutability, and the authors walk through converting Java collection code carefully, including nullability and generic-type pitfalls during automated conversion. - **Distinguish calculations from actions** (Middle): A useful rule of thumb is to reserve computed properties for calculations (results that depend only on unchanging data), while actions depend on when and how often they run. - **Static methods become top-level functions, objects, and companions** (Middle): This refactoring reflects a genuine grain difference between the languages and changes how you organize utility code. - **Functions can become extension functions and properties** (Middle): Moving from methods to properties and from functions to extensions makes code more expressive and is a recurring theme in the refactoring legs. - **The book is deliberately not exhaustive** (Opening): The authors state that coroutines are barely mentioned because they haven't changed how they write server-side code, and topics like builders, DSLs, reflection, DI frameworks, and transactions are left out. 【Reading Tips】 - **Deep-read the refactoring chapters**: The value is in the step-by-step transformations (Optional to Nullable, Beans to Values, Static Methods to Top-Level Functions). Skim the introductory history if you already know Java's evolution. - **Watch for the "grain of Kotlin" arguments**: The authors repeatedly explain *why* Kotlin differs from Java, not just *how* to convert. These explanations are where the real learning happens. - **Use the book as a reference during real migrations**: The chapters are organized as independent legs, so you can jump to the one matching your current refactoring task. - **Don't expect coroutines or DI guidance**: If those are your priority, this book explicitly does not cover them; look elsewhere. - **Pay attention to the calculation-vs-action distinction**: It's a small rule with large design implications for how you structure Kotlin code. 【Coverage Limits】 This guide is based on stratified excerpts covering roughly the first half of the book, including the introduction, migration strategy, nullability, beans-to-values, collections, and early function refactorings. Later chapters (streams to sequences, sealed classes, I/O, and the ending) are only partially represented, so details of those legs are not fully covered here.
Excerpt 1
53 Refactoring Beans to Values 53 Moving On 60 6. Java to Kotlin Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
View in text
Excerpt 2
irst step of the journey from pure Java to a mixed and then increasingly Kotlin codebase? Strategy The first time we, Nat and Duncan, introduced Kotlin to a...
View in text
Excerpt 3
is required. In contrast, T is not a subtype of Optional<T>. If you have a String and want to assign it to an optional variable, you first have to wrap it in...
View in text
Excerpt 4
aring { obj: Journey -> obj.duration }.reversed() ).subList(0, actualLimit) } fun routesToShowFor(itineraryId: String?): List<List<Journey>> { return bearabl...
View in text
Excerpt 5
).collect(toUnmodifiableList()) } ... etc. Example 8.5 [static-to-top-level.6:src/main/java/travelator/Shortlists.kt] This keeps our Java happy, but irritati...
View in text
Excerpt 6
oise of the ?.: val reminder: String? = customer.nextTrip().reminderAt(currentTime) On the other hand, we’ve made the flow of nullability in the calling func...
View in text
Excerpt 7
property is simple and makes our code easier to understand. 158 | Chapter 11: Methods to Properties Conventions for Denoting Values The static of functions o...
View in text
Excerpt 8
h asSequence().groupBy {...}; the lambda remains unchanged: val bookingEventsByInteractionId = eventsForSuccessfulBookings .asSequence() .groupBy { event ->
View in text
Tags
AI categories
JavaProgramming LanguageSoftware
ISBN: 1492082279
Publisher: O'Reilly Media
Publish Year: 2021
Language: English
Pages: 556
File Format: PDF
File Size: 7.8 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…