Praise for Java to Kotlin
Nat and Duncan open the book by saying they wish this book had existed when they adopted Kotlin. I completely agree. The book takes their experiences, their trials and errors , and helps you, the reader, to be on the right path. It definitely is a book that should be on anyones shelf coming to Kotlin from the JVM ecosystem. An enjoyable read, and an even better reference guide!
Hadi Hariri, JetBrains
To take familiar good old Java code and see it transform, incrementally, into concise, clear, expressive and easy to maintain Kotlin code is a wonderful way to learn the language. Benefit from the experience McGregor and Pryce have distilled in this book.
Dr. Venkat Subramaniam, award-winning author and founder of Agile Developer, Inc.
The fastest way to upgrade your skills from Java to Kotlin. Essential reading for any professional Java developer.
Dawn Griffiths and David Griffiths, authors of Head First Kotlin
Java to Kotlin
by Duncan McGregor and Nat Pryce
Copyright 2021 Duncan McGregor and Nat Pryce. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Suzanne McQuade
- Development Editor: Sarah Grey
- Production Editor: Kate Galloway
- Copyeditor: nSight, Inc.
- Proofreader: Sonia Saruba
- Indexer: Judith McConville
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- August 2021: First Edition
Revision History for the First Edition
- 2021-08-13: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492082279 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Java to Kotlin, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the authors, and do not represent the publishers views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-08227-9
[LSI]
Preface
Hello, this is Duncan and Nat.As youre reading this preface, youre probably trying to decide whether to invest some hours into reading the rest of this book.So lets cut to the chase:
This book wont teach you to program computers in Kotlin.
We started writing a book that would, but it soon became clear that Kotlin is a large language, and so the book was going to take longer to write than we wanted.There are also already some great books in that space, and we dont like competing against great.
We decided instead to make our lives easier by concentrating on teaching Kotlin to Java developers, based on a workshop that we run called Refactoring to Kotlin.This teaches the Kotlin language by converting existing code and is (according to our marketing material) designed for Java teams wanting to leverage their existing knowledge to accelerate their Kotlin adoption.
We started writing that book, but it soon became clear that Kotlin is still a large language, and so we would still be writing for a long time.We also found that motivated and experienced Java developers can pick up most of Kotlin very quickly.It felt patronizing to plow our way through language features that our target readers will probably just appreciate and adopt as soon as they see them.So we abandoned that idea, and as a result:
This book wont teach you the Kotlin language.
So why should you read it?Because we have written the book we wish was available when we first adopted Kotlin.We are experienced programmers who know Java and the Java ecosystem well.We hope you are too.Like us, you probably have experience in a number of other languages.Youve learned the basics of Kotlin, and you recognize that to get the best out of the language you will need to design your systems differently.You have found that some things that are cumbersome in Java are much easier in Kotlin, and that some features, such as checked exceptions, are not there at all.You dont want to end up merely writing Java code in Kotlin syntax.
Perhaps you have skin in the game.Maybe youre in a technical leadership position, or have successfully convinced your team to adopt Kotlin.You might have spent some political capital to get Kotlin into the project.Now you need to ensure that the transition goes smoothly.
You may be responsible for a Java codebase and want to ensure that introducing Kotlin wont destabilize its existing, business-critical code.Or you may be starting a Kotlin project from scratch but realize your design instincts turn more readily to Java and objects than to Kotlin and functions.
If this is you, as it was us, then youve come to the right place.This book will help you adapt your thinking and designs to take advantage of Kotlin.Thats not enough, though, because you have existing code that you need to maintain and enhance.So we also show how to migrate that code from Java to Kotlin syntax, and from Java to Kotlin thinking, incrementally and safely, using the automated refactoring tools built into the IntelliJ IDE.
How This Book Is Organized
This book is about how to transition from Java to Kotlin, mainly focused on code but touching on projects and organizations.Each chapter addresses an aspect of this transition, looking at some aspect of typical Java projects that can be improved on the journey.They are named in the pattern Java Way to Kotlin Way, where we recommend that you prefer the latter over the former.Maybe Kotlin makes easier an approach that was difficult in Java, or Kotlin discourages an approach that is common in Java to guide design in a direction that is less error-prone, more concise, and more tool-friendly.
We dont just recommend you adopt the Kotlin way though; the chapters also show how to make the transformation.Not by just rewriting the Java, but by gradually refactoring it to Kotlin it in a way that is safe and allows us to maintain a mixed language codebase.
How Did We Choose the Topics?
We began by analyzing Java and Kotlin developers use of their respective languages and conducting interviews to identify areas of difference and confusion.This was backed by a machine learning analysis of 33,459 open source Java and Kotlin codebases.These identified candidates that we labeled in the thing-to-another-thing form before ranking them according to frequency and developer-pain-quotient in order to establish which should make the cut.Finally, we ordered the surviving topics by