ebook img

Galata I., Howard J., Lucas R. - Kotlin Apprentice PDF

418 Pages·2000·15.6 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Galata I., Howard J., Lucas R. - Kotlin Apprentice

Kotlin Apprentice Kotlin Apprentice By Irina Galata, Joe Howard, Richard Lucas & Ellen Shapiro Copyright ©2018 Razeware LLC. Notice 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. Notice 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. raywenderlich.com 2 Kotlin Apprentice About the team Irina Galata is an author of this book. Irina is a software developer in Dnipro, Ukraine. She is passionate about Android, animations, public speeches and Kotlin. You can follow her on Twitter at @igalata13. Joe Howard is an author and final pass editor of this book. Joe is a former physicist that studied computational particle physics using parallel Fortran simulations. He gradually shifted into systems engineering and then 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. Richard Lucas is an author and tech editor of this book. Richard 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. Ellen Shapiro is an author of this book. Ellen is an iOS developer for Bakken & Bæck's Amsterdam office who also occasionally writes Android apps. Ellen has written and edited tutorials and books for RayWenderlich.com since 2013. She is working in her spare time to help bring songwriting app Hum (https://justhum.com) to life. 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. Chris Belanger is an editor of this book. Chris is the Editor in Chief at raywenderlich.com. He was a developer for nearly 20 years in various fields from e-health to aerial surveillance to industrial controls. If there are words to wrangle or a paragraph to ponder, he’s on the case. When he kicks back, you can usually find Chris with guitar in hand, looking for the nearest beach. Twitter: @crispytwit. raywenderlich.com 3 Kotlin Apprentice Manda Frederick is an editor of this book. Manda has been involved in publishing for over ten years through various creative, educational, medical and technical print and digital publications. In her free time, you can find her at the climbing gym, backpacking in the backcountry, hanging with her dog, working on poems, playing guitar and exploring breweries. We’d also like to acknowledge the efforts of the authors of the Swift 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. raywenderlich.com 4 Kotlin Apprentice Dedications "To my fiancée Lilia, for all her support, encouragement, and patience. Love you. 😻 " —Ellen Shapiro "To my family for all the support that I got during the writing of this book. And to Dnipro Kotlin community, as my passion to Kotlin began due to these guys." —Irina Galata "To Lauren, Isabel, Rafael and Sasha. Keep changing, but don't ever change." —Joe Howard "To everyone who said I would make it." —Richard Lucas raywenderlich.com 5 Kotlin Apprentice Table of Contents: Overview Introduction ............................................................................. 16 Book License.................................................................... 17 Book Source Code & Forums .................................... 18 What You Need............................................................... 19 About the Cover............................................................. 20 Section I: Kotlin Basics ......................................................... 21 Chapter 1: Your Kotlin Development Environment.................................................................... 23 Chapter 2: Expressions, Variables & Constants 35 Chapter 3: Types & Operations................................ 57 Chapter 4: Basic Control Flow ................................. 74 Chapter 5: Advanced Control Flow........................ 90 Chapter 6: Functions ................................................. 103 Chapter 7: Nullability................................................ 114 Section II: Collections & Lambdas................................. 125 Chapter 8: Arrays & Lists......................................... 126 Chapter 9: Maps & Sets ............................................ 141 Chapter 10: Lambdas ................................................ 152 raywenderlich.com 6 Kotlin Apprentice Section III: Building Your Own Types........................... 164 Chapter 11: Classes ................................................... 166 Chapter 12: Objects................................................... 179 Chapter 13: Properties............................................. 189 Chapter 14: Methods ................................................ 204 Chapter 15: Advanced Classes.............................. 214 Chapter 16: Enum Classes....................................... 231 Chapter 17: Interfaces.............................................. 249 Chapter 18: Generics ................................................ 259 Section IV: Intermediate Topics..................................... 294 Chapter 19: Kotlin/Java Interoperability.......... 296 Chapter 20: Exceptions ............................................ 323 Chapter 21: Functional Programming................ 334 Chapter 22: Conventions & Operator Overloading................................................................... 357 Chapter 23: Kotlin Coroutines.............................. 369 Chapter 24: Scripting with Kotlin......................... 384 Appendix A: Kotlin Platforms......................................... 409 Conclusion.............................................................................. 418 raywenderlich.com 7 Kotlin Apprentice Table of Contents: Extended Introduction ............................................................................. 16 Book License ............................................................................ 17 Book Source Code & Forums............................................. 18 What You Need....................................................................... 19 About the Cover ..................................................................... 20 Section I: Kotlin Basics ......................................................... 21 Chapter 1: Your Kotlin Development Environment. 23 Getting started with IntelliJ IDEA............................................................................... 23 Book sample projects....................................................................................................... 30 Key points............................................................................................................................. 33 Where to go from here?.................................................................................................. 34 Challenges............................................................................................................................ 34 Chapter 2: Expressions, Variables & Constants......... 35 How a computer works................................................................................................... 35 Getting started with Kotlin............................................................................................ 42 Printing out.......................................................................................................................... 43 Arithmetic operations...................................................................................................... 44 Math functions.................................................................................................................... 48 Naming data......................................................................................................................... 49 Increment and decrement.............................................................................................. 53 Key points............................................................................................................................. 54 Where to go from here?.................................................................................................. 55 Challenges............................................................................................................................ 55 Chapter 3: Types & Operations ........................................ 57 Type conversion.................................................................................................................. 57 Strings..................................................................................................................................... 61 raywenderlich.com 8 Kotlin Apprentice Strings in Kotlin.................................................................................................................. 63 Pairs and Triples................................................................................................................. 66 Number types...................................................................................................................... 69 Any, Unit, and Nothing Types........................................................................................ 70 Key points............................................................................................................................. 71 Where to go from here?.................................................................................................. 72 Challenges............................................................................................................................ 72 Chapter 4: Basic Control Flow.......................................... 74 Comparison operators..................................................................................................... 74 The if expression................................................................................................................ 78 Loops....................................................................................................................................... 83 Key points............................................................................................................................. 87 Where to go from here?.................................................................................................. 87 Challenges............................................................................................................................ 88 Chapter 5: Advanced Control Flow ................................ 90 Ranges.................................................................................................................................... 90 For loops................................................................................................................................ 91 when expressions.............................................................................................................. 96 Key points........................................................................................................................... 100 Where to go from here?................................................................................................ 101 Challenges.......................................................................................................................... 101 Chapter 6: Functions.......................................................... 103 Function basics................................................................................................................. 103 Functions as variables................................................................................................... 108 Key points........................................................................................................................... 111 Where to go from here?................................................................................................ 112 Challenges.......................................................................................................................... 112 Chapter 7: Nullability......................................................... 114 Introducing null................................................................................................................ 114 Introducing nullable types........................................................................................... 116 Checking for null............................................................................................................. 117 raywenderlich.com 9 Kotlin Apprentice Safe calls............................................................................................................................. 120 Elvis operator.................................................................................................................... 121 Key points........................................................................................................................... 122 Where to go from here?................................................................................................ 122 Challenges.......................................................................................................................... 123 Section II: Collections & Lambdas................................. 125 Chapter 8: Arrays & Lists.................................................. 126 Arrays................................................................................................................................... 126 Lists....................................................................................................................................... 128 Accessing elements........................................................................................................ 130 Modifying lists.................................................................................................................. 133 Updating elements......................................................................................................... 135 Iterating through a list.................................................................................................. 136 Nullability and collection types................................................................................. 137 Key points........................................................................................................................... 138 Where to go from here?................................................................................................ 139 Challenges.......................................................................................................................... 139 Chapter 9: Maps & Sets..................................................... 141 Creating maps.................................................................................................................. 142 Accessing values.............................................................................................................. 143 Modifying mutable maps.............................................................................................. 144 Iterating through maps................................................................................................. 146 Running time for map operations............................................................................. 146 Key points........................................................................................................................... 147 Sets....................................................................................................................................... 147 Creating sets..................................................................................................................... 148 Accessing elements........................................................................................................ 148 Adding and removing elements................................................................................. 149 Running time for set operations................................................................................ 149 Key points........................................................................................................................... 149 Where to go from here?................................................................................................ 150 Challenges.......................................................................................................................... 150 raywenderlich.com 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.