0% found this document useful (0 votes)
129 views44 pages

(FREE PDF Sample) Kotlin Apprentice Second Edition Beginning Programming With Kotlin Irina Galata Ebooks

Kotlin

Uploaded by

funchasserrf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views44 pages

(FREE PDF Sample) Kotlin Apprentice Second Edition Beginning Programming With Kotlin Irina Galata Ebooks

Kotlin

Uploaded by

funchasserrf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Kotlin Apprentice Second Edition Beginning


Programming with Kotlin Irina Galata

https://textbookfull.com/product/kotlin-apprentice-second-
edition-beginning-programming-with-kotlin-irina-galata/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Android Apprentice Second Edition Beginning Android


Development with Kotlin Darryl Bayliss

https://textbookfull.com/product/android-apprentice-second-edition-
beginning-android-development-with-kotlin-darryl-bayliss/

textboxfull.com

Android Apprentice Fourth Edition Beginning Android


Development with Kotlin Namrata Bandekar

https://textbookfull.com/product/android-apprentice-fourth-edition-
beginning-android-development-with-kotlin-namrata-bandekar/

textboxfull.com

Programming Kotlin Applications Building Mobile and Server


Side Applications with Kotlin 1st Edition Brett Mclaughlin

https://textbookfull.com/product/programming-kotlin-applications-
building-mobile-and-server-side-applications-with-kotlin-1st-edition-
brett-mclaughlin/
textboxfull.com

Programming Kotlin Enhance your skills for Android


development using Kotlin 2nd Edition Alexander Aronowitz

https://textbookfull.com/product/programming-kotlin-enhance-your-
skills-for-android-development-using-kotlin-2nd-edition-alexander-
aronowitz/
textboxfull.com
Programming Android With Kotlin: Achieving Structured
Concurrency With Coroutines Pierre-Olivier Laurence

https://textbookfull.com/product/programming-android-with-kotlin-
achieving-structured-concurrency-with-coroutines-pierre-olivier-
laurence/
textboxfull.com

Programming DSLs in Kotlin 1st Edition Venkat Subramaniam


[Subramaniam

https://textbookfull.com/product/programming-dsls-in-kotlin-1st-
edition-venkat-subramaniam-subramaniam/

textboxfull.com

Reactive Programming with Kotlin First Edition Learn Rx


with RxJava RxKotlin and RXAndroid Alex Sullivan

https://textbookfull.com/product/reactive-programming-with-kotlin-
first-edition-learn-rx-with-rxjava-rxkotlin-and-rxandroid-alex-
sullivan/
textboxfull.com

Kotlin Programming: The Big Nerd Ranch Guide 1st Edition


Josh Skeen

https://textbookfull.com/product/kotlin-programming-the-big-nerd-
ranch-guide-1st-edition-josh-skeen/

textboxfull.com

Data Structures Algorithms in Kotlin Implementing


Practical Data Structures in Kotlin 1st Edition
Raywenderlich Tutorial Team
https://textbookfull.com/product/data-structures-algorithms-in-kotlin-
implementing-practical-data-structures-in-kotlin-1st-edition-
raywenderlich-tutorial-team/
textboxfull.com
Kotlin Apprentice

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.

raywenderlich.com 2
Kotlin Apprentice

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.

raywenderlich.com 3
Kotlin Apprentice

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.

raywenderlich.com 4
Kotlin Apprentice

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.

raywenderlich.com 5
Kotlin Apprentice

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

raywenderlich.com 6
Kotlin Apprentice

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

raywenderlich.com 7
Kotlin Apprentice

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

raywenderlich.com 8
Kotlin Apprentice

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

raywenderlich.com 9
Kotlin Apprentice

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

raywenderlich.com 10
Kotlin Apprentice

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

raywenderlich.com 11
Kotlin Apprentice

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

raywenderlich.com 12
Kotlin Apprentice

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

raywenderlich.com 13
Kotlin Apprentice

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

raywenderlich.com 14
Kotlin Apprentice

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

raywenderlich.com 15
Kotlin Apprentice

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

raywenderlich.com 16
Kotlin Apprentice

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

raywenderlich.com 17
I Introduc,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
L Book 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
B Book 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
Kotlin Apprentice Book Source Code & Forums

Visit our Kotlin Apprentice store page here:

• https://store.raywenderlich.com/products/kotlin-apprentice.

And if you purchased the print version of this book, you’re eligible to upgrade to the
digital editions at a significant discount! Simply email [email protected] with
your receipt for the physical copy and we’ll get you set up with the discounted digital
edition version of the book.

raywenderlich.com 21
W What You Need

To follow along with this book, you'll need the following:

• IntelliJ IDEA Community Edition 2019.1.3 or later, available at https://


www.jetbrains.com/idea/. This is the environment in which you'll develop the
sample code in this book.

• Kotlin Plugin for IntelliJ IDEA v1.3.40 or later, installed (if not included by
default) by going to IntelliJ IDEA Preferences on macOS (or Settings on
Windows/Linux) and choosing Plugins, then searching on "Kotlin".

• Java SE Development Kit 8 or later. Most of the code in this book will be run on
the Java Virtual Machine or JVM, for which you need a Java Development Kit or
JDK. The JDK can be downloaded from Oracle at http://www.oracle.com/
technetwork/java/javase/downloads/index.html.

• In Chapters 24 and 25, you'll have the option of using a text editor such as Visual
Studio Code instead of IntelliJ IDEA.

• In Chapter 26, in place of IntelliJ IDEA, you'll use Android Studio 3.5 or later and
Xcode 10.3 or later to build a Kotlin Multiplatform app.

If you haven't installed the latest versions of IntelliJ IDEA Community Edition, the
Kotlin Plugin, and JDK 8, be sure to do that before continuing with the book. Chapter
1: "Your Kotlin Development Environment" will show you how to get started with
IntelliJ IDEA to run Kotlin code on the JVM.

raywenderlich.com 22
A About the Cover

The parrot has long captured the eye and imagination of humans; from Aesop to
Monty Python, from ancient Peruvians to swashbuckling pirates, the beautiful
plumage of parrots have made them a recurring theme throughout history.

The expressiveness and mimicry of parrots is very much akin to Kotlin; Kotlin takes
the best of many languages and “mimics" those languages in a modern and
expressive way. Although Kotlin won’t whistle a tune or ride on your shoulder when
you cosplay Long John Silver, Kotlin will still be a great companion to you as you sail
the seven seas of programming!

raywenderlich.com 23
Sec,on I: Kotlin Basics

The chapters in this section will introduce you to the very basics of programming in
Kotlin. From the fundamentals of how computers work, all the way up to language
structures, you’ll cover enough of the language to be able to work with data and
organize your code’s behavior.

The section begins with some groundwork to get you started:

• Chapter 1, Your Kotlin Development Environment: First things first, you'll


need somewhere to program in the Kotlin language and work with the sample
projects for this book. To start your Kotlin projects and run the code on your
machine, you'll primarily use a tool called IntelliJ IDEA from JetBrains. This
chapter will introduce you to this Integrated Development Environment to work
with the code examples in this book.

• Chapter 2, Expressions, Variables & Constants: You'll need to learn some basics
about how code works so that you can begin programming in Kotlin. You’ll begin
with an overview of computers and programming. You’ll learn some basics such as
code comments, arithmetic operations, constants and variables. These are some of
the fundamental building blocks of any language, and Kotlin is no different.

• Chapter 3, Types & Operations: You’ll learn about handling different types,
including strings, which allow you to represent text. You’ll learn about converting
between types, and you’ll also be introduced to type inference, which makes your
life as a programmer a lot simpler. You’ll learn how to group multiple values of any
type into pairs and triples.

raywenderlich.com 24
Kotlin Apprentice Section I: Kotlin Basics

Once you have the basic data types in your head, it’ll be time to do things with that
data:

• Chapter 4, Basic Control Flow: Because your computer doesn't know what to do
unless you tell it, you'll need to understand how to control the flow of a program
by various methods. You’ll learn how to make decisions and repeat tasks in your
programs by using syntax to control the flow. You’ll also learn about Booleans,
which represent true and false values, and how you can use these to compare data.
And, finally, you'll learn about loops, which are Kotlin’s way of executing code
multiple times.

• Chapter 5, Advanced Flow Control: Continuing the theme of code not running
in a straight line, you’ll learn about another loop known as the for loop. And,
you'll learn about when expressions, which are particularly powerful in Kotlin; they
let you inspect a value and decide what to do based on that value. They’re
incredibly powerful when used with pattern matching.

• Chapter 6, Functions: Functions are the basic building blocks you use to
structure your code in Kotlin. You’ll learn how to define functions to group your
code into reusable units.

And, finally, you'll move past the variables and constants you've been working with,
which have had concrete values:

• Chapter 7, Nullability: You'll learn about nullable types, with which you can
represent a value but also the absence of a value. They can be tricky but, by the end
of the chapter, you'll know why you would use nullable types and when best to use
them effectively.

These fundamentals will get you on your way and, before you know it, you’ll be ready
for the more advanced topics that follow. Let’s get started!

raywenderlich.com 25
1 Chapter 1: Your Kotlin
Development Environment
By Joe Howard

Welcome to Kotlin Apprentice! In this first chapter, you’re going to set up a


development environment to let you program in the Kotlin language and work with
the sample projects for each chapter in the book.

Then, you’ll write your very first Kotlin code and see how to run the code on your
machine.

The primary tool that you’ll use in this book to create Kotlin projects is IntelliJ IDEA
from JetBrains. JetBrains is also the company behind the Kotlin language itself, so
Kotlin development is very tightly integrated into IntelliJ IDEA.

IntelliJ IDEA is an Integrated Development Environment, or IDE, and is similar to


other IDEs such as Visual Studio and Xcode. IntelliJ IDEA provides the foundation
of many other IDEs from JetBrains, including Android Studio for Android app
development, PyCharm for Python programming and CLion for C and C++
programming.

You use an IDE to write code in an editor, compile the code into a form that can be
run on your computer, see output from your program, fix issues in your code and
much more! You’ll just scratch the surface of the power of IntelliJ IDEA in this
chapter, but you’ll be setup to work with the code examples throughout the rest of
the book.

raywenderlich.com 26
Kotlin Apprentice Chapter 1: Your Kotlin Development Environment

GeTng started with IntelliJ IDEA


You can download IntelliJ IDEA from the JetBrains website at https://
www.jetbrains.com/idea/. There are both Community and Ultimate editions of the
IDE; you’ll just need the Community edition to work with the code in this book. The
Community edition is a free download.

Go ahead and download IntelliJ IDEA 2019.2 or later on your platform of choice.
There are versions for macOS, Windows and Linux. Follow the installation
instructions on the JetBrains site to install IntelliJ IDEA on your machine. Most of
the screenshots in this book will be from the macOS version, but the Windows and
Linux versions are similar.

Before you first run IntelliJ IDEA, you’ll also want to install a Java Development
Kit, or JDK, which will easily let you run Kotlin code on your machine.

Java and the JDK


Kotlin allows you to program on a number of different platforms. The two most
prominent platforms are the Java Virtual Machine, or JVM, and Android. See
Appendix A: "Kotlin Platforms" for more information on all the different platforms
that Kotlin runs on.

In many ways, Kotlin was initially created as a modern replacement for the Java
programming language. Java was created in the 1990’s as an early attempt at a cross-
platform application language, promising a "Write Once, Run Everywhere" approach
to software development.

Instead of compiling to native machine code on each platform, Java programs are
compiled into a format called bytecode. The bytecode runs inside an application on
the Java Virtual Machine. The JVM can be thought of as a layer above your actual
machine. By running as bytecode on a virtual machine, you are able to share Java
code and applications across many types of computer systems.

One of the goals of the Kotlin programming languages is to be 100% interoperable


with the Java language. This includes Kotlin code being converted to Java-compatible
bytecode by the Kotlin compiler, so that the Kotlin code can be run on the JVM.

Most of the code and projects in this book are meant to be run as Kotlin projects on
the JVM. In order to do so, you must install the JDK alongside IntelliJ IDEA. The
easiest way to get a JDK for your platform is to visit the Oracle website at:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

raywenderlich.com 27
Kotlin Apprentice Chapter 1: Your Kotlin Development Environment

You’ll want to download and install the latest version of the JDK — at least version 8.
The Java software tools go by the name "Java SE," which includes the JDK and also
the Java Runtime Environment, or JRE.

Note: Be sure to download and install the JDK and not just the JRE, since the
JRE only lets you run Java applications and does not include the tools to build
new ones.

Running IntelliJ IDEA


Once you’ve installed IntelliJ IDEA and the JDK, follow the normal process of
starting the IntelliJ IDEA application on your platform.

If you’ve installed previous versions of IntelliJ IDEA on the same machine, the
installer will likely prompt you to import settings from a previous version. If you’ve
not installed previous versions on the same machine, you’ll be prompted to choose a
color theme and choose plugins to install into the IDE. You can just choose the
default settings and proceed.

You’ll then arrive at the Welcome to IntelliJ IDEA window.

From the welcome window, you can create new projects, import or open existing
projects, check out code from a version control system such as Git, run configuration
tools and get help on the IDE.

raywenderlich.com 28
Kotlin Apprentice Chapter 1: Your Kotlin Development Environment

Your first project


Go ahead and choose Create New Project on the welcome screen. You’ll see the first
of two project configuration screens.

Choose Kotlin in the list of options on the left and Kotlin JVM as the project type
and click Next.

You'll see the following:

You then see a screen for the project name and location. You also see the Project
SDK, which should be the JDK version that you installed earlier — or a different JDK
version if you have more than one installed on your machine.

raywenderlich.com 29
Exploring the Variety of Random
Documents with Different Content
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project
Gutenberg™ works in compliance with the terms of this
agreement for keeping the Project Gutenberg™ name
associated with the work. You can easily comply with the terms
of this agreement by keeping this work in the same format with
its attached full Project Gutenberg™ License when you share it
without charge with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.

1.E. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it
away or re-use it under the terms of the Project Gutenberg
License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country where
you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of the
copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.
1.E.5. Do not copy, display, perform, distribute or redistribute
this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite
these efforts, Project Gutenberg™ electronic works, and the
medium on which they may be stored, may contain “Defects,”
such as, but not limited to, incomplete, inaccurate or corrupt
data, transcription errors, a copyright or other intellectual
property infringement, a defective or damaged disk or other
medium, a computer virus, or computer codes that damage or
cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES -


Except for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU
AGREE THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE,
STRICT LIABILITY, BREACH OF WARRANTY OR BREACH
OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE
TRADEMARK OWNER, AND ANY DISTRIBUTOR UNDER
THIS AGREEMENT WILL NOT BE LIABLE TO YOU FOR
ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE
OR INCIDENTAL DAMAGES EVEN IF YOU GIVE NOTICE OF
THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If


you discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person or
entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR
ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you do
or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status by
the Internal Revenue Service. The Foundation’s EIN or federal
tax identification number is 64-6221541. Contributions to the
Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or
determine the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like