For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email:
2017 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Development editor: Dan MaharryReview editor: Aleksandar DragosavljeviTechnical development editor: Brent WatsonProject editor: Kevin SullivanCopyeditor: Tiffany TaylorProofreader: Elizabeth MartinTechnical proofreader: Igor WojdaTypesetter: Marija TudorCover designer: Marija Tudor
Foreword
When I visited JetBrains for the first time in Spring 2010, I came in fairly certain that the world didnt need another general-purpose programming language. I thought that existing JVM languages were good enough, and who in their right mind creates a new language anyway? After about an hour discussing production issues in large-scale codebases I was convinced otherwise, and the first ideas that later became part of Kotlin were sketched on a whiteboard. I joined JetBrains shortly after to lead the design of the language and work on the compiler.
Today, more than six years later, we have our second release approaching. There are over 30 people on the team and thousands of active users, and we still have more exciting design ideas than I can handle easily. But dont worry, those ideas have to pass a rather thorough examination before they get into the language. We want Kotlin of the future to still fit into a single reasonably sized book.
Learning a programming language is an exciting and often very rewarding endeavor. If its your first one, youre learning the whole new world of programming through it. If its not, it makes you think about familiar things in new terms and thus understand them more deeply and on a higher level of abstraction. This book is primarily targeted for the latter kind of reader, those already familiar with Java.
Designing a language from scratch may be a challenging task in its own right, but making it play well with another is a different storyone with many angry ogres in it, and some gloomy dungeons too. (Ask Bjarne Stroustrup, the creator of C++, if you dont trust me on that.) Java interoperability (that is, how Java and Kotlin can mix and call each other) was one of the cornerstones of Kotlin, and this book pays a lot of attention to it. Interoperability is very important for introducing Kotlin gradually to an existing Java codebase. Even when writing a new project from scratch, one has to fit the language into the bigger picture of the platform with all of its libraries written in Java.
As Im writing this, two new target platforms are being developed: Kotlin is now running on JavaScript VMs enabling full-stack web development, and it will soon be able to compile directly to native code and run without any VM at all, if necessary. So, while this book is JVM-oriented, much of what you learn from it can be applied to other execution environments.
The authors have been members of the Kotlin team from its early days, so they are intimately familiar with the language and its internals. Their experience in conference presentations, workshops, and courses about Kotlin has enabled them to deliver good explanations that anticipate common questions and possible pitfalls. The book explains high-level concepts behind language features and provides all the necessary details as well.
I hope youll enjoy your time with our language and this book. As I often say in our community postings: Have a nice Kotlin!
A NDREY B RESLAV
L EAD D ESIGNER OF K OTLIN AT J ETBRAINS
Preface
The idea of Kotlin was conceived at JetBrains in 2010. By that time, JetBrains was an established vendor of development tools for many languages, including Java, C#, JavaScript, Python, Ruby, and PHP. IntelliJ IDEA, the Java IDE that is our flagship product, also included plugins for Groovy and Scala.
The experience of building the tooling for such a diverse set of languages gave us a unique understanding of and perspective on the language design space as a whole. And yet the IntelliJ Platform-based IDEs, including IntelliJ IDEA, were still being developed in Java. We were somewhat envious of our colleagues on the .NET team who were developing in C#, a modern, powerful, rapidly evolving language. But we didnt see any language that we could use instead of Java.
What were our requirements for such a language? The first and most obvious was static typing. We dont know any other way to develop a multimillion-line codebase over many years without going crazy. Second, we needed full compatibility with the existing Java code. That codebase is a hugely valuable asset for JetBrains, and we couldnt afford to lose it or devalue it through difficulties with interoperability. Third, we didnt want to accept any compromises in terms of tooling quality. Developer productivity is the most important value for JetBrains as a company, and great tooling is essential to achieving that. Finally we needed a language that was easy to learn and to reason about.
When we see an unmet need for our company, we know there are other companies in similar situations, and we expect that our solution will find many users outside of JetBrains. With this in mind, we decided to embark on the project of creating a new language: Kotlin. As it happens, the project took longer than we expected, and Kotlin 1.0 came out more than five years after the first commit to the repository; but now we can be certain that the language has found its audience and is here to stay.
Kotlin is named after an island near St. Petersburg, Russia, where most of the Kotlin development team is located. In using an island name, we followed the precedent established by Java and Ceylon, but we decided to go for something closer to our homes. (In English, the name is usually pronounced cot-lin, not coat-lin or caught-lin.)
As the language was approaching release, we realized that it would be valuable to have a book about Kotlin, written by people who were involved in making design decisions for the language and who could confidently explain why things in Kotlin are the way they are. This book is a result of that effort, and we hope it will help you learn and understand the Kotlin language. Good luck, and may you always develop with pleasure!