Kotlin Programming: The Big Nerd Ranch Guide
by Andrew Bailey , David Greenhalgh and Josh Skeen
Copyright 2021 Big Nerd Ranch, LLC
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, contact
Big Nerd Ranch, LLC
200 Arizona Ave NE
Atlanta, GA 30307
(770) 817-6373
http://www.bignerdranch.com/
book-comments@bignerdranch.com
The 10-gallon hat with propeller logo is a trademark of Big Nerd Ranch, LLC.
Exclusive worldwide distribution of the English edition of this book by
Pearson Technology Group
800 East 96th Street
Indianapolis, IN 46240 USA
http://www.informit.com
The authors and publisher have taken care in writing and printing this book but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
ISBN-10 0136870481
ISBN-13 978-0136870487
Second edition, first printing, September 2021
Release E.2.1.1
Dedication
To Mom and Dad. Of all the words in this book, the ones that do justice to the support and mentorship youve given me over the years were the hardest to find. These two will have to suffice: Thank you. |
A.B. |
To Rebecca, a driven, patient, beautiful woman, and the reason that this book came to be. To Mom and Dad, for valuing education above all else. |
D.G. |
For Baker, the best little bug. |
J.S. |
Acknowledgments
Although our names appear on the cover, a book is not made by its authors alone. Many people have contributed to the creation of this book. We owe our thanks to everyone who helped make this book what it is today:
Bryan Sills, Michael Yotive, Nate Sottek, Jeremy Sherman, and Mark Duran, for graciously lending your eyes and offering feedback on updates for the second edition of this book.
Eric Maxwell, for teaching an early version of the second edition and providing feedback about our coroutines, channels, and flows chapters.
Loren Klingman and Jake Sower, for your feedback on the Kotlin/JS chapter.
Drew Fitzpatrick, for reading through an early version of the second edition in its entirety and for your feedback on our Kotlin Multiplatform and Kotlin/Native content.
Liv Vitale, Christian Keur, Zachary Waldowski, and David House, for constantly offering your expertise on other platforms, which helped greatly with our Kotlin/JS and Kotlin/Native discussions. Thank you for tolerating the deluge of oddly specific questions and for making us look smarter than we really are.
Javontay McElroy, our talented Big Nerd Ranch designer who made the print books IntelliJ IDEA cheat sheet. Thank you for enthusiastically diving head-first into the unfamiliar territory of printed material and for making this design from scratch.
Eric Wilson, Madison Witzler, Franklin ONeal, and CJ Best, who together are the masterminds of Big Nerd Ranchs training department. Our classes and, by extension, this book could not happen without the hard work you do.
Our editor, Elizabeth Holaday. When we told you our plans for the second edition of this book, the first thing you said was, So this is a pretty substantial update. You would, of course, turn out to be absolutely correct in that assessment. Thank you for all your hard work along this journey to refine this book, amplify its strengths, and shore up its weaknesses.
Simone Payment, our copyeditor and proofreader. Thank you for your help in putting the finishing touches on this book.
Ellie Volckhausen, who designed our front cover. Surfs up!
Chris Loper at IntelligentEnglish.com, who designed and produced the print and digital versions of this book. We also extensively leveraged his DocBook toolchain.
Aaron Hillegass and Stacy Henry. As a practical matter, it would not be possible for this book to exist without Big Nerd Ranch, the company Aaron founded and Stacy now fearlessly leads.
Finally, thank you to all our students. Being your teacher offers us the opportunity to be a student in many ways, and for that we are immensely grateful. Teaching is part of the greatest thing that we do, and it has been a pleasure working with you. We hope that the quality of this book matches your enthusiasm and determination.
Introducing Kotlin
In 2011, JetBrains announced the development of the Kotlin programming language, an alternative to writing code in languages like Java or Scala to run on the Java Virtual Machine. Six years later, Google announced that Kotlin would be an officially supported development path for the Android operating system.
Kotlins scope quickly grew from a language with a bright future into the language powering applications on the worlds foremost mobile operating system. Today, large companies like Google, Uber, Netflix, Capital One, Amazon, and more have embraced Kotlin for its many advantages, including its concise syntax, modern features, and seamless interoperability with legacy Java code.
Why Kotlin?
To understand the appeal of Kotlin, you first need to understand the role of Java in the modern software development landscape. The two languages are closely tied, because Kotlin code is most often written for the Java Virtual Machine.
Java is a robust and time-tested language and has been one of the most commonly written languages in production codebases for years. However, since Java was released in 1995, much has been learned about what makes for a good programming language. Java is missing the many advancements that developers working with more modern languages enjoy.
Kotlin benefits from the learning gained as some design decisions made in Java (and other languages, like Scala) have aged poorly. It has evolved beyond what was possible with older languages and has corrected what was painful about them. You will learn more in the coming chapters about how Kotlin improves on Java and offers a more reliable development experience.
And Kotlin is not just a better language to write code to run on the Java Virtual Machine. It is a multiplatform language that aims to be general purpose: Kotlin can be used to write native macOS, iOS, and Windows applications; JavaScript applications; and, of course, Android applications. Recently, JetBrains has been investing in these cross-platform capabilities; Kotlin Multiplatform offers a unique way to share code across applications and has led to an increase in Kotlins use beyond the Java Virtual Machine.
Who Is This Book For?
We have written this book for developers of all kinds: experienced Android developers who want modern features beyond what Java offers, server-side developers interested in learning about Kotlins features, developers looking to share Kotlin code between their native or web apps, and newer developers looking to venture into a high-performance compiled language.