Introduction....18
Book License....19
Book Source Code & Forums....20
What You Need....22
About the Cover....23
Chapter 1: Your Kotlin Development Environment....26
Getting 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
Getting started with Kotlin....46
Printing out....47
Arithmetic operations....48
Math functions....53
Naming data....54
Increment and decrement....57
Challenges....58
Key points....60
Where to go from here?....61
Chapter 3: Types & Operations....62
Type conversion....63
Strings....67
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: Functions....110
Function basics....111
Functions 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
Checking for null....126
Safe calls....129
Elvis operator....130
Challenges....131
Key points....132
Where to go from here?....132
Chapter 8: Arrays & Lists....134
Arrays....134
Lists....138
Accessing elements....140
Modifying lists....144
Updating elements....146
Iterating through a list....147
Nullability and collection types....148
Challenges....149
Key points....151
Where to go from here?....151
Chapter 9: Maps & Sets....152
Creating maps....153
Accessing values....154
Modifying mutable maps....155
Iterating through maps....157
Running time for map operations....157
Key points....158
Sets....159
Creating sets....159
Accessing elements....160
Adding and removing elements....160
Running time for set operations....160
Challenges....161
Key points....162
Where to go from here?....162
Chapter 10: Lambdas....163
Lambdas basics....164
Custom sorting with lambdas....169
Iterating over collections with lambdas....169
Challenges....173
Key points....175
Where to go from here?....175
Chapter 11: Classes....178
Creating 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
Getting started....193
Using static members....196
Using anonymous objects....199
Challenges....200
Key points....201
Where to go from here?....201
Chapter 13: Properties....202
Constructor properties....203
Property initializers....204
Custom accessors....205
Companion object properties....208
Delegated properties....210
lateinit....213
Extension properties....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
Creating your first enum class....247
Enum class properties and functions....251
Using when with enum classes....255
Sealed classes vs. enum classes....256
Enumeration 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
Implementing multiple 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 functions on types with generic constraints....282
Creating your own generic constraints....284
Generic interfaces....293
Type erasure....296
Generic type variance (a.k.a., in and out declarations)....304
Challenges....309
Key points....311
Where to go from here?....311
Chapter 19: Kotlin/Java Interoperability....314
Mixing Java and Kotlin code....315
Java nullability annotations....326
Making your Kotlin Code Java-friendly....330
Accessing nested Kotlin objects....334
"Static" values and functions from Kotlin....337
Challenge....340
Key points....341
Where to go from here?....341
Chapter 20: Exceptions....342
What is an exception?....343
Throwing exceptions....345
Handling exceptions....347
Creating custom exceptions....348
Difference between Java and Kotlin exceptions....351
Challenges....351
Key points....352
Where to go from here?....353
Chapter 21: Functional Programming....354
What is functional programming?....355
Robot battle!....356
First-class and higher-order functions....359
Lambdas....362
Extension functions....366
Lambdas with receivers....367
Anonymous functions....368
Returning from lambdas....369
Inline functions....370
Tail recursive functions....373
Collections standard library....374
Infix notation....375
Sequences....375
Challenges....377
Key points....377
Where to go from here?....378
Chapter 22: Conventions & Operator Overloading....379
What is operator overloading?....380
Getting started....380
Using conventions....381
Handling collections....385
Operator overloading and Java....389
Delegated properties as conventions....389
Challenges....390
Key points....390
Where to go from here?....391
Chapter 23: Kotlin Coroutines....392
Asynchronous programming....393
Coroutines....394
Getting started....395
Configuring coroutines....396
CoroutineScope....397
Coroutines builders....398
Example: A high-rise building....401
Error handling....407
Understanding coroutines....407
Challenges....408
Key points....408
Where to go from here?....409
Chapter 24: Scripting with Kotlin....410
What is scripting?....411
Installing Kotlin for scripting....412
Using the REPL....414
Creating script files....417
Handling arguments....424
Getting information from the system....427
Challenges....433
Key points....434
Where to go from here?....434
Chapter 25: Kotlin/Native....435
Konan and LLVM....436
Installation....436
Hello, Kotlin/Native....438
Kotlin Standard Library....440
Challenges....440
Key points....441
Where to go from here?....442
Chapter 26: Kotlin Multiplatform....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 Platforms....464
Kotlin on the JVM....465
Android....465
Kotlin on the server....470
Kotlin to JavaScript....470
Kotlin/Native and Multiplatform....471
Where to go from here?....472
Conclusion....474
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.
This book is for complete beginners to Kotlin. No prior programming experience is necessary!
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!