Statistics
5
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-02-15

AuthorIrina Galata, Joe Howard, Ellen Shapiro

Learn how to program with Kotlin! Kotlin is the exciting modern language from JetBrains, creators of IntelliJ IDEA, the basis of many popular IDEs such as Android Studio and PyCharm. Since the adoption of Kotlin by Google as an official language for Android, the momentum behind Kotlin has gone off the charts. Kotlin supports many platforms, including Android, the web, the back-end, and even iOS. By reading this book, you'll be ready to use Kotlin on any and all of these platforms. Who This Book Is For This book is for complete beginners to Kotlin. No prior programming experience is necessary! Topics Covered in Kotlin Apprentice Kotlin Development Environment: See how to setup a development environment for Kotlin using IntelliJ IDEA. Numbers and Strings: These are the basic kinds of data in any app-learn how to use them in Kotlin. Making Decisions: Your code doesn't always run straight through-learn how to use conditions and loops to control program flow. Functions and Lambdas: Group your code together into reusable chunks to run and pass around. Collection Types: Discover the many ways Kotlin offers to store and organize data into collections. Building Your Own Types: Learn how to model elements in your app using classes, objects, interfaces, and enumerations. Functional Programming: Learn how to use Kotlin in a functional style and how this can make your code clearer and more efficient. Coroutines: Asynchronous programming can be a complex topic on any platform, but Kotlin gives you a clear and consise approach with coroutines. Kotlin Platforms and Scripting: Learn about how Kotlin can be used on multiple platforms and see its use as a scripting language. Kotlin/Native and Multiplatform: See how to use Kotlin/Native to bring your apps to more than one platform. One thing you can count on: after reading this book, you’ll be prepared to take advantage of Kotlin wherever you choose to use it!

Tags
No tags
ISBN: 1950325008
Publisher: Razeware LLC
Publish Year: 2019
Language: 英文
Pages: 475
File Format: PDF
File Size: 20.9 MB
Support Statistics
¥.00 · 0times
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.

(This page has no text content)
Kotlin Appren,ce By Irina Galata, Joe Howard & Ellen Shapiro Copyright ©2019 Razeware LLC. No,ce of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. No,ce of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. Kotlin Apprentice raywenderlich.com 2
About the team Irina Galata is an author of this book. is a software engineer in Linz, Austria, working at Runtastic. She passionate about programming and exploring new technologies. You can follow her on twitter @igalata13. Joe Howard is an author and final pass editor of this book. Joe is a computational physicist who studied particle physics using parallel Fortran simulations. He gradually shifted into systems engineering and ultimately software engineering around the time of the release of the iOS and Android SDKs. He's been a mobile software developer on iOS and Android since 2009, working primarily at two agencies in Boston, MA since 2011. He's now the Android Pillar Lead for raywenderlich.com. Twitter: @orionthewake. Ellen Shapiro is an author of this book. works for Apollo GraphQL, caring and feeding for their iOS SDK. She's been building iOS and Android apps since late 2010, and has written and edited tutorials and books about iOS and Android for RayWenderlich.com since 2013. She’s also developed several independent applications through her personal company, Designated Nerd Software. When she's not writing code, she's usually tweeting about it at @DesignatedNerd. Kotlin Apprentice raywenderlich.com 3
Acknowledgements We’d also like to acknowledge the efforts of the authors of the Swift Apprentice and previous editions of Kotlin Apprentice whose work formed the basis for parts of this book: • Janie Clayton is an independent iOS developer. She spent a year apprenticed to a super genius programming robots and learning the forgotten ways of long term software maintenance and development. Janie is the coauthor on several books on iOS and Swift development. Janie lives outside of Madison, Wisconsin with her attempted grumble of pugs and multitude of programming books. Janie writes her various musings on her blog at RedQueenCoder.com. • Alexis Gallagher is a software engineer who is always looking for the conceptual deep dive and always hoping to find pearls down at the bottom. When he’s not coding, he’s out and about in sunny San Francisco. • Matt Galloway is a software engineer with a passion for excellence. He stumbled into iOS programming when it first was a thing, and has never looked back. When not coding, he likes to brew his own beer. • Eli Ganim is an engineering manager at Facebook. He is passionate about teaching, writing, and sharing his knowledge with others. • Erik Kerber is a software developer in Minneapolis, MN, and the lead iOS developer for the Target app. He does his best to balance a life behind the keyboard with cycling, hiking, scuba diving, and traveling. • Ben Morrow delights in discovering the unspoken nature of the world. He’ll tell you the surprising bits while on a walk. He produces beauty by drawing out the raw wisdom that exists within each of us. • Cosmin Pupăză is a software developer and tutorial writer from Romania. He has worked with more than a dozen programming languages over the years, but none of them has made such a great impact on himself as the advent of Swift. When not coding, he either plays the guitar or studies WWII history. Cosmin blogs about Swift at cosminpupaza.wordpress.com. • Steven Van Impe is a computer science lecturer at the University College of Ghent, Belgium. When he’s not teaching, Steven can be found on his bike, rattling over cobblestones and sweating up hills, or relaxing around the table, enjoying board games with friends. You can find Steven on Twitter as @svanimpe. • Dick Lucas is a developer by trade but adds value anyway he can. He is also a writer, podcast host, and advisor at nogradient.com. He thinks most things are superfluous. Kotlin Apprentice raywenderlich.com 4
About the Ar,st Vicki Wenderlich is the designer and artist of the cover of this book. She is Ray’s wife and business partner. She is a digital artist who creates illustrations, game art and a lot of other art or design work for the tutorials and books on raywenderlich.com. When she’s not making art, she loves hiking, a good glass of wine and attempting to create the perfect cheese plate. Kotlin Apprentice raywenderlich.com 5
Dedica,ons "To my fiancée Lilia, for all her support, encouragement, and patience. Love you. ! " —Ellen Shapiro "To my loved ones for their support." —Irina Galata "To Lauren." —Joe Howard Kotlin Apprentice raywenderlich.com 6
Table of Contents: Overview Introduc7on 18..................................................................................... Book License 19........................................................................ Book Source Code & Forums 20........................................... What You Need 22.................................................................... About the Cover 23.................................................................. Sec,on I: Kotlin Basics 24................................................... Chapter 1: Your Kotlin Development Environment 26..... Chapter 2: Expressions, Variables & Constants 39............ Chapter 3: Types & Opera7ons 62........................................ Chapter 4: Basic Control Flow 80.......................................... Chapter 5: Advanced Control Flow 97................................. Chapter 6: Func7ons 110........................................................ Chapter 7: Nullability 122....................................................... Sec,on II: Collec,ons & Lambdas 133............................ Chapter 8: Arrays & Lists 134................................................. Chapter 9: Maps & Sets 152................................................... Chapter 10: Lambdas 163....................................................... Sec,on III: Building Your Own Types 176....................... Chapter 11: Classes 178.......................................................... Chapter 12: Objects 191......................................................... Chapter 13: Proper7es 202.................................................... Kotlin Apprentice raywenderlich.com 7
Chapter 14: Methods 217....................................................... Chapter 15: Advanced Classes 228...................................... Chapter 16: Enum Classes 246.............................................. Chapter 17: Interfaces 265..................................................... Chapter 18: Generics 275....................................................... Sec,on IV: Intermediate Topics 312................................. Chapter 19: Kotlin/Java Interoperability 314..................... Chapter 20: Excep7ons 342................................................... Chapter 21: Func7onal Programming 354.......................... Chapter 22: Conven7ons & Operator Overloading 379.. Chapter 23: Kotlin Corou7nes 392....................................... Chapter 24: Scrip7ng with Kotlin 410.................................. Chapter 25: Kotlin/Na7ve 435.............................................. Chapter 26: Kotlin Mul7pla^orm 443.................................. Appendix A: Kotlin Pla^orms 464.................................................... Conclusion 474..................................................................................... Kotlin Apprentice raywenderlich.com 8
Table of Contents: Extended Introduc7on 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book License 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 20. . . . . . . . . . . . . . . . . . . . . . . . . . What You Need 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Cover 23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec,on I: Kotlin Basics 24. . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1: Your Kotlin Development Environment 26. . . . . . . . Ge_ng started with IntelliJ IDEA 27. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book sample projects 33. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 37. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Expressions, Variables & Constants 39. . . . . . . . . . . How a computer works 40. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ge_ng started with Kotlin 46. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Prin7ng out 47. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arithme7c opera7ons 48. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Math func7ons 53. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Naming data 54. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Increment and decrement 57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 60. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 61. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Types & Opera7ons 62. . . . . . . . . . . . . . . . . . . . . . . . . Type conversion 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings 67. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 9
Strings in Kotlin 69. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pairs and Triples 72. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number types 75. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Any, Unit, and Nothing Types 76. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 77. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 78. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 79. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Basic Control Flow 80. . . . . . . . . . . . . . . . . . . . . . . . . Comparison operators 81. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The if expression 85. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Loops 90. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 94. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 95. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 96. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Advanced Control Flow 97. . . . . . . . . . . . . . . . . . . . . Ranges 98. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . For loops 98. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . when expressions 103. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 108. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 109. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 109. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Func7ons 110. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Func7on basics 111. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Func7ons as variables 116. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 119. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: Nullability 122. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing null 123. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing nullable types 124. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 10
Checking for null 126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Safe calls 129. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Elvis operator 130. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 131. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec,on II: Collec,ons & Lambdas 133. . . . . . . . . . . . . . Chapter 8: Arrays & Lists 134. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays 134. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lists 138. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing elements 140. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying lists 144. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Upda7ng elements 146. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Itera7ng through a list 147. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nullability and collec7on types 148. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 149. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 151. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 151. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 9: Maps & Sets 152. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Crea7ng maps 153. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing values 154. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying mutable maps 155. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Itera7ng through maps 157. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running 7me for map opera7ons 157. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 158. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sets 159. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Crea7ng sets 159. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing elements 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding and removing elements 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running 7me for set opera7ons 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 11
Challenges 161. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 10: Lambdas 163. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lambdas basics 164. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom sor7ng with lambdas 169. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Itera7ng over collec7ons with lambdas 169. . . . . . . . . . . . . . . . . . . . . . . . Challenges 173. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 175. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 175. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec,on III: Building Your Own Types 176. . . . . . . . . . . . Chapter 11: Classes 178. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Crea7ng classes 178. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reference types 179. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding state and side effects 185. . . . . . . . . . . . . . . . . . . . . . . . . . Data classes 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 189. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 190. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 190. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 12: Objects 191. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Singletons 192. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ge_ng started 193. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using sta7c members 196. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using anonymous objects 199. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 200. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Proper7es 202. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constructor proper7es 203. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 12
Property ini7alizers 204. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom accessors 205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Companion object proper7es 208. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Delegated proper7es 210. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . lateinit 213. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extension proper7es 214. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 215. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 216. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 216. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 14: Methods 217. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Method refresher 218. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing this 220. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Object methods 221. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extension methods 223. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 226. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 227. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 227. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 15: Advanced Classes 228. . . . . . . . . . . . . . . . . . . . . . . . Introducing inheritance 229. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sealed classes 237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Secondary constructors 238. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nested and inner classes 239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Visibility modifiers 240. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . When and why to subclass 242. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 244. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 245. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 245. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 16: Enum Classes 246. . . . . . . . . . . . . . . . . . . . . . . . . . . . Crea7ng your first enum class 247. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enum class proper7es and func7ons 251. . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 13
Using when with enum classes 255. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sealed classes vs. enum classes 256. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enumera7on as state machine 260. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nullables and enums 262. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 263. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 264. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 264. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 17: Interfaces 265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing interfaces 266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interface inheritance 270. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implemen7ng mul7ple interfaces 271. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interfaces in the standard library 271. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 273. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 274. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 274. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 18: Generics 275. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anatomy of standard library generic types 276. . . . . . . . . . . . . . . . . . . . . Extension func7ons on types with generic constraints 282. . . . . . . . . . Crea7ng your own generic constraints 284. . . . . . . . . . . . . . . . . . . . . . . . Generic interfaces 293. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Type erasure 296. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generic type variance (a.k.a., in and out declara7ons) 304. . . . . . . . . . . Challenges 309. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 311. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 311. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sec,on IV: Intermediate Topics 312. . . . . . . . . . . . . . . . Chapter 19: Kotlin/Java Interoperability 314. . . . . . . . . . . . . . . . Mixing Java and Kotlin code 315. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java nullability annota7ons 326. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Making your Kotlin Code Java-friendly 330. . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 14
Accessing nested Kotlin objects 334. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "Sta7c" values and func7ons from Kotlin 337. . . . . . . . . . . . . . . . . . . . . . Challenge 340. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 341. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 341. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 20: Excep7ons 342. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is an excep7on? 343. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Throwing excep7ons 345. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling excep7ons 347. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Crea7ng custom excep7ons 348. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Difference between Java and Kotlin excep7ons 351. . . . . . . . . . . . . . . . . Challenges 351. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 352. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 353. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 21: Func7onal Programming 354. . . . . . . . . . . . . . . . . . What is func7onal programming? 355. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Robot bakle! 356. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . First-class and higher-order func7ons 359. . . . . . . . . . . . . . . . . . . . . . . . . Lambdas 362. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extension func7ons 366. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lambdas with receivers 367. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anonymous func7ons 368. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning from lambdas 369. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inline func7ons 370. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tail recursive func7ons 373. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collec7ons standard library 374. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Infix nota7on 375. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sequences 375. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 377. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 377. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 378. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 15
Chapter 22: Conven7ons & Operator Overloading 379. . . . . . What is operator overloading? 380. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ge_ng started 380. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using conven7ons 381. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling collec7ons 385. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operator overloading and Java 389. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Delegated proper7es as conven7ons 389. . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 390. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 390. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 391. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 23: Kotlin Corou7nes 392. . . . . . . . . . . . . . . . . . . . . . . . Asynchronous programming 393. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Corou7nes 394. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ge_ng started 395. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring corou7nes 396. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Corou7neScope 397. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Corou7nes builders 398. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Example: A high-rise building 401. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error handling 407. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding corou7nes 407. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 408. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 408. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 409. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 24: Scrip7ng with Kotlin 410. . . . . . . . . . . . . . . . . . . . . . What is scrip7ng? 411. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Kotlin for scrip7ng 412. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the REPL 414. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Crea7ng script files 417. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling arguments 424. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ge_ng informa7on from the system 427. . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 433. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 16
Key points 434. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 434. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 25: Kotlin/Na7ve 435. . . . . . . . . . . . . . . . . . . . . . . . . . . . Konan and LLVM 436. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installa7on 436. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello, Kotlin/Na7ve 438. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Standard Library 440. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 440. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 441. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 442. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 26: Kotlin Mul7pla^orm 443. . . . . . . . . . . . . . . . . . . . . . The KMP Approach 444. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HelloKMP 447. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared project 449. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . expect and actual 452. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared code from Android 455. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The iOS app 456. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared code from iOS 460. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 463. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 463. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 463. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appendix A: Kotlin Pla^orms 464. . . . . . . . . . . . . . . . . . . . . . . . . Kotlin on the JVM 465. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Android 465. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin on the server 470. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin to JavaScript 470. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin/Na7ve and Mul7pla^orm 471. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 472. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 474. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 17
IIntroduc,on The Kotlin language has been around since 2011, but its popularity took off in 2017 when Google announced Kotlin’s inclusion as a first-class language for Android development. In 2019, Google announced a "Kotlin-first" approach to Android development. With modern and expressive language characteristics such as those found in Apple’s Swift, and 100% interoperability with Java, it’s no wonder that Kotlin has been named a top 5 most-loved language by Stack Overflow users. If you're a complete beginner to programming, this is the book for you! There are short exercises and challenges throughout the book to give you some programming practice and test your knowledge along the way. Through Kotlin Apprentice, you'll learn about basic things like constants, values and types, move up to more complicated items such as data structures, classes and enumerators, and finish off with some in-depth knowledge about functional programming, coroutines, and Kotlin Multiplatform. If you want to get right into Android app development after you work through Kotlin Apprentice, we suggest you read the Android Apprentice, available on our store: • https://store.raywenderlich.com/products/android-apprentice The Android Apprentice is your introduction to building great apps in Android, using the Kotlin language, for both novice programmers and those with extensive experience programming for iOS or other platforms. It will help you master the essential building blocks of Kotlin and Android to start creating apps. As you work on more apps, you'll find the foundations you learn in Kotlin Apprentice and Android Apprentice will give you the knowledge you need to easily figure out more complicated details on your own. raywenderlich.com 18
LBook License By purchasing Kotlin Apprentice, you have the following license: • You are allowed to use and/or modify the source code in Kotlin Apprentice in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in Kotlin Apprentice in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from Kotlin Apprentice, available at www.raywenderlich.com”. • The source code included in Kotlin Apprentice is for your personal use only. You are NOT allowed to distribute or sell the source code in Kotlin Apprentice without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. All trademarks and registered trademarks appearing in this guide are the properties of their respective owners. raywenderlich.com 19
BBook Source Code & Forums If you bought the digital edi,on The digital edition of this book comes with the source code for the starter and completed projects for each chapter. These resources are included with the digital edition you downloaded from store.raywenderlich.com. If you bought the print version You can get the source code for the print edition of the book here: https://store.raywenderlich.com/products/kotlin-apprentice-source-code Forums We’ve also set up an official forum for the book at forums.raywenderlich.com. This is a great place to ask questions about the book or to submit any errors you may find. Digital book edi,ons We have a digital edition of this book available in both ePUB and PDF, which can be handy if you want a soft copy to take with you, or you want to quickly search for a specific term within the book. Buying the digital edition version of the book also has a few extra benefits: free updates each time we update the book, access to older versions of the book, and you can download the digital editions from anywhere, at anytime. raywenderlich.com 20