𝔖 Scriptorium
✦   LIBER   ✦

📁

Head first Kotlin: a brain-friendly guide

✍ Scribed by Griffiths, Dawn;Griffiths, David


Publisher
O'Reilly Media
Year
2019
Tongue
English
Leaves
770
Series
Head first series
Category
Library

⬇  Acquire This Volume

No coin nor oath required. For personal study only.

✦ Synopsis


What will you learn from this book?

Head First Kotlinis a complete introduction to coding in Kotlin. This hands-on book helps you learn the Kotlin language with a unique method that goes beyond syntax and how-to manuals and teaches you how to think like a great Kotlin developer. You'll learn everything from language fundamentals to collections, generics, lambdas, and higher-order functions. Along the way, you'll get to play with both object-oriented and functional programming. If you want to really understand Kotlin, this is the book for you.

Why does this book look so different?

Based on the latest research in cognitive science and learning theory,Head First Kotlinuses a visually rich format to engage your mind rather than a text-heavy approach that puts you to sleep. Why waste your time struggling with new concepts? This multisensory learning experience is designed for the way your brain really works.

✦ Table of Contents


how to use this book: Intro......Page 8
Who is this book for?......Page 10
Who should probably back away from this book?......Page 11
We know what your brain is thinking......Page 12
Metacognition: thinking about thinking......Page 16
Here’s what WE did:......Page 18
Here’s what YOU can do to bend your brain into submission......Page 19
Read me......Page 22
The technical review team......Page 23
Acknowledgments......Page 25
O’Reilly......Page 27
Table of Contents (the real thing)......Page 28
1. getting started: A Quick Dip......Page 47
You can use object-oriented AND functional programming......Page 48
The compiler keeps you safe......Page 49
Client-side and server-side JavaScript......Page 50
Native apps......Page 51
What we’ll do in this chapter......Page 52
Install IntelliJ IDEA (Community Edition)......Page 55
Let’s build a basic application......Page 57
2. Specify the type of project......Page 58
3. Configure the project......Page 59
You’ve just created your first Kotlin project......Page 60
Add a new Kotlin file to the project......Page 62
Anatomy of the main function......Page 63
Add the main function to App.kt......Page 66
Test drive......Page 67
What the Run command does......Page 68
What can you say in the main function?......Page 69
Loop and loop and loop.........Page 71
Simple boolean tests......Page 72
A loopy example......Page 73
Test drive......Page 74
Conditional branching......Page 75
Using if to return a value......Page 77
Update the main function......Page 78
Test drive......Page 79
Code Magnets......Page 80
Using the Kotlin interactive shell......Page 81
You can add multi-line code snippets to the REPL......Page 83
It’s exercise time......Page 84
Code Magnets Solution......Page 92
Your Kotlin Toolbox......Page 93
2. basic types and variables: Being a Variable......Page 96
A variable is like a cup......Page 97
What happens when you declare a variable......Page 98
...and the compiler infers the variable’s type from that of the object......Page 99
val vs. var revisited......Page 100
Integers......Page 101
Booleans......Page 103
Characters and Strings......Page 104
How to explicitly declare a variable’s type......Page 105
Declaring the type AND assigning a value......Page 106
Use the right value for the variable’s type......Page 107
Assigning a value to another variable......Page 108
We need to convert the value......Page 110
An object has state and behavior......Page 111
What happens when you convert a value......Page 112
Watch out for overspill......Page 113
How to create an array......Page 118
Create the Phrase-O-Matic application......Page 119
Add the code to PhraseOMatic.kt......Page 120
The compiler infers the array’s type from its values......Page 124
var means the variable can point to a different array......Page 125
...but you can still update the variables in the array......Page 127
Code Magnets......Page 129
Code Magnets Solution......Page 133
Your Kotlin Toolbox......Page 136
3. functions: Getting Out of Main......Page 139
How the game will work......Page 140
A high-level design of the game......Page 141
Get started: create the project......Page 143
Create the Rock, Paper, Scissors array......Page 144
How you create functions......Page 145
You can send things to a function......Page 146
You can send more than one thing to a function......Page 147
You can pass variables to a function so long as the variable type matches the parameter type......Page 148
Functions with no return value......Page 149
Functions with single-expression bodies......Page 150
Code Magnets......Page 151
Add the getGameChoice function to Game.kt......Page 152
Behind the scenes: what happens......Page 153
The story continues......Page 154
The getUserChoice function......Page 162
Ask for the user’s choice......Page 163
Looping through a range of numbers......Page 164
Use downTo to reverse the range......Page 166
Looping through the items in an array......Page 167
Use the readLine function to read the user’s input......Page 168
We need to validate the user’s input......Page 172
‘And’ and ‘Or’ operators (&& and ||)......Page 173
Not equals (!= and !)......Page 174
Add the getUserChoice function to Game.kt......Page 175
We need to print the results......Page 177
Add the printResult function to Game.kt......Page 180
Test drive......Page 182
Your Kotlin Toolbox......Page 183
4. classes and objects: A Bit of Class......Page 185
Object types are defined using classes......Page 187
How to design your own classes......Page 188
Let’s define a Dog class......Page 189
How to create a Dog object......Page 191
What if the Dog is in a Dog array?......Page 192
Create a Songs application......Page 193
Test drive......Page 194
The miracle of object creation......Page 195
How objects are created......Page 196
Behind the scenes: calling the Dog constructor......Page 197
Code Magnets......Page 203
Code Magnets Solution......Page 205
Going deeper into properties......Page 206
Behind the scenes of the Dog constructor......Page 207
Flexible property initialization......Page 208
How to use initializer blocks......Page 209
You MUST initialize your properties......Page 210
The solution: custom getters and setters......Page 216
How to write a custom getter......Page 217
How to write a custom setter......Page 219
The full code for the Dogs project......Page 221
Test drive......Page 224
Your Kotlin Toolbox......Page 230
5. subclasses and superclasses: Using Your Inheritance......Page 232
An inheritance example......Page 233
What we’re going to do......Page 235
Design an animal class inheritance structure......Page 236
Use inheritance to avoid duplicate code in subclasses......Page 238
What should the subclasses override?......Page 239
The animals have different property values.........Page 240
We can group some of the animals......Page 241
Add Canine and Feline classes......Page 243
Use IS-A to test your class hierarchy......Page 245
The IS-A test works anywhere in the inheritance tree......Page 247
We’ll create some Kotlin animals......Page 253
Declare the superclass and its properties and functions as open......Page 255
How a subclass inherits from a superclass......Page 256
How (and when) to override properties......Page 258
Overriding properties lets you do more than assign default values......Page 260
How to override functions......Page 262
The rules for overriding functions......Page 263
An overridden function or property stays open.........Page 264
...until it’s declared final......Page 265
Add the Hippo class to the Animals project......Page 266
Code Magnets......Page 267
Code Magnets Solution......Page 268
Add the Canine and Wolf classes......Page 270
Which function is called?......Page 271
Inheritance guarantees that all subclasses have the functions and properties defined in the superclass......Page 273
Any place where you can use a superclass, you can use one of its subclasses instead......Page 274
When you call a function on the variable, it’s the object’s version that responds......Page 275
You can use a supertype for a function’s parameters and return type......Page 277
The updated Animals code......Page 278
Test drive......Page 280
Your Kotlin Toolbox......Page 286
6. abstract classes and interfaces: Serious Polymorphism......Page 289
The Animal class hierarchy revisited......Page 290
Some classes shouldn’t be instantiated......Page 291
Declare a class as abstract to stop it from being instantiated......Page 292
Abstract or concrete?......Page 293
An abstract class can have abstract properties and functions......Page 294
The Animal class has two abstract functions......Page 295
How to implement an abstract class......Page 299
You MUST implement all abstract properties and functions......Page 301
Let’s update the Animals project......Page 304
Test drive......Page 307
Independent classes can have common behavior......Page 312
An interface lets you define common behavior OUTSIDE a superclass hierarchy......Page 313
Interface functions can be abstract or concrete......Page 315
How to define interface properties......Page 316
Declare that a class implements an interface.........Page 317
...then override its properties and functions......Page 318
How to implement multiple interfaces......Page 319
How do you know whether to make a class, a subclass, an abstract class, or an interface?......Page 321
Update the Animals project......Page 322
Test drive......Page 325
Access uncommon behavior by checking an object’s type......Page 329
As the condition for an if......Page 330
In a while loop......Page 331
Use when to compare a variable against a bunch of options......Page 332
The is operator usually performs a smart cast......Page 334
Use as to perform an explicit cast......Page 335
Test drive......Page 337
Your Kotlin Toolbox......Page 341
7. data classes: Dealing with Data......Page 343
== calls a function named equals......Page 344
equals is inherited from a superclass named Any......Page 345
The importance of being Any......Page 346
The common behavior defined by Any......Page 347
We might want equals to check whether two objects are equivalent......Page 349
How to create objects from a data class......Page 350
The equals function compares property values......Page 351
toString returns the value of each property......Page 352
Copy data objects using the copy function......Page 353
Data classes define componentN functions.........Page 354
...that let you destructure data objects......Page 355
Create the Recipes project......Page 358
Test drive......Page 359
Generated functions only use properties defined in the constructor......Page 364
Default parameter values to the rescue!......Page 367
1. Passing values in order of declaration......Page 368
2. Using named arguments......Page 369
Functions can use default values too......Page 373
Overloading a function......Page 374
Dos and don’ts for function overloading:......Page 375
Let’s update the Recipes project......Page 376
Test drive......Page 377
Your Kotlin Toolbox......Page 382
8. nulls and exceptions: Safe and Sound......Page 385
How do you remove object references from variables?......Page 386
Remove an object reference using null......Page 387
Why have nullable types?......Page 388
You can use a nullable type everywhere you can use a non-nullable type......Page 389
How to create an array of nullable types......Page 391
How to access a nullable type’s functions and properties......Page 392
Keep things safe with safe calls......Page 394
What happens when a safe call chain gets evaluated......Page 396
The story continues......Page 397
You can use safe calls to assign values.........Page 398
...and assign values to safe calls......Page 399
Use let to run code if values are not null......Page 403
Using let to streamline expressions......Page 405
...you can use the safer Elvis operator......Page 407
The !! operator deliberately throws a NullPointerException......Page 409
Create the Null Values project......Page 410
The code continued.........Page 411
Test drive......Page 412
An exception is thrown in exceptional circumstances......Page 417
Catch exceptions using a try/catch......Page 418
Use finally for the things you want to do no matter what......Page 420
An exception is an object of type Exception......Page 422
You can explicitly throw exceptions......Page 428
How to use throw as an expression......Page 430
Code Magnets......Page 432
Code Magnets Solution......Page 435
Your Kotlin Toolbox......Page 437
9. collections: Get Organized......Page 439
Arrays can be useful.........Page 440
Arrays are mutable, so they can be updated......Page 442
When in doubt, go to the Library......Page 443
List, Set and Map......Page 444
Set - when uniqueness matters......Page 445
Fantastic Lists.........Page 446
...and how to use them......Page 447
Create a MutableList.........Page 448
..and add values to it......Page 449
You can remove a value.........Page 450
You can change the order and make bulk changes.........Page 453
...or take a copy of the entire MutableList......Page 454
Test drive......Page 456
Code Magnets......Page 457
Code Magnets Solution......Page 458
Lists allow duplicate values......Page 459
How to create a Set......Page 462
How to use a Set’s values......Page 463
How a Set checks for duplicates......Page 464
Hash codes and equality......Page 466
Equality using the == operator......Page 467
Rules for overriding hashCode and equals......Page 468
How to use a MutableSet......Page 470
You can copy a MutableSet......Page 472
Test drive......Page 474
How to create a Map......Page 484
How to use a Map......Page 486
Create a MutableMap......Page 487
Put entries in a MutableMap......Page 488
You can remove entries from a MutableMap......Page 489
You can copy Maps and MutableMaps......Page 490
The full code for the Collections project......Page 491
Test drive......Page 492
Your Kotlin Toolbox......Page 501
10. generics: Know Your Ins from Your Outs......Page 503
Collections use generics......Page 504
Understanding collection documentation (Or, what’s the meaning of “E”?)......Page 505
Using type parameters with MutableList......Page 507
Things you can do with a generic class or interface......Page 508
Here’s what we’re going to do......Page 509
Create the Pet class hierarchy......Page 511
Define the Contest class......Page 512
You can restrict T to a specific supertype......Page 513
Create the addScore function......Page 514
Create the getWinners function......Page 515
Create some Contest objects......Page 516
The compiler can infer the generic type......Page 517
Create the Generics project......Page 520
Test drive......Page 522
The Retailer hierarchy......Page 527
Define the Retailer interface......Page 528
...but what about polymorphism?......Page 531
Use out to make a generic type covariant......Page 532
Collections are defined using covariant types......Page 533
Update the Generics project......Page 534
Test drive......Page 536
We need a Vet class......Page 539
Assign a Vet to a Contest......Page 540
Create Vet objects......Page 541
Pass a Vet to the Contest constructor......Page 542
Use in to make a generic type contravariant......Page 543
A generic type can be locally contravariant......Page 544
Update the Generics project......Page 546
Test drive......Page 549
Your Kotlin Toolbox......Page 556
11. lambdas and higher-order functions: Treating Code Like Data......Page 558
Introducing lambdas......Page 559
What we’re going to do......Page 560
What lambda code looks like......Page 561
You can assign a lambda to a variable......Page 563
Execute a lambda’s code by invoking it......Page 564
What happens when you invoke a lambda......Page 565
Lambda expressions have a type......Page 567
You can replace a single parameter with it......Page 570
Use the right lambda for the variable’s type......Page 571
Use Unit to say a lambda has no return value......Page 572
Create the Lambdas project......Page 573
Test drive......Page 574
You can pass a lambda to a function......Page 579
Add a lambda parameter to a function by specifying its name and type......Page 580
What happens when you call the function......Page 581
...or remove the ()’s entirely......Page 585
Update the Lambdas project......Page 586
Test drive......Page 587
A function can return a lambda......Page 591
Write a function that receives AND returns lambdas......Page 592
How to use the combine function......Page 593
What happens when the code runs......Page 594
Use typealias to provide a different name for an existing type......Page 598
Update the Lambdas project......Page 600
Test drive......Page 601
Code Magnets......Page 602
Code Magnets Solution......Page 605
Your Kotlin Toolbox......Page 610
12. built-in higher-order functions: Power Up Your Code......Page 612
Kotlin has a bunch of built-in higher-order functions......Page 613
The minBy and maxBy functions work with ALL types......Page 614
A closer look at minBy and maxBy’s lambda parameter......Page 616
The sumBy and sumByDouble functions......Page 617
sumBy and sumByDouble’s lambda parameter......Page 618
Create the Groceries project......Page 619
Test drive......Page 620
Meet the filter function......Page 623
There’s a whole FAMILY of filter functions......Page 624
Use map to apply a transform to your collection......Page 625
You can chain function calls together......Page 626
What happens when the code runs......Page 627
The story continues.........Page 628
forEach works like a for loop......Page 629
Lambdas have access to variables......Page 631
Update the Groceries project......Page 632
Test drive......Page 634
Use groupBy to split your collection into groups......Page 639
You can use groupBy in function call chains......Page 640
How to use the fold function......Page 642
Behind the scenes: the fold function......Page 643
Subtract the total price of items from an initial value......Page 646
Update the Groceries project......Page 647
Test drive......Page 648
Your Kotlin Toolbox......Page 656
Leaving town.........Page 657
It’s been great having you here in Kotlinville......Page 658
A. coroutines: Running Code in Parallel......Page 659
1. Create a new GRADLE project......Page 660
2. Enter an artifact ID......Page 661
4. Specify the project name......Page 662
Add the code to the project......Page 663
Test drive......Page 664
Use coroutines to make beats play in parallel......Page 665
2. Launch a coroutine......Page 666
A coroutine is like a lightweight thread......Page 667
Use runBlocking to run coroutines in the same scope......Page 668
Thread.sleep pauses the current THREAD......Page 669
The delay function pauses the current COROUTINE......Page 670
The full project code......Page 671
Test drive......Page 672
B. testing: Hold Your Code to Account......Page 674
Add the JUnit library......Page 675
Create a JUnit test class......Page 676
Using KotlinTest......Page 678
Use rows to test against sets of data......Page 679
C. leftovers: The Top Ten Things: (We Didn’t Cover)......Page 680
How to add a package......Page 681
Package declarations......Page 682
The fully qualified name......Page 683
Type the fully qualified name.........Page 684
Visibility modifiers and top level code......Page 685
Visibility modifiers and classes/interfaces......Page 687
3. Enum classes......Page 688
enum properties and functions......Page 689
Sealed classes to the rescue!......Page 691
How to use sealed classes......Page 692
5. Nested and inner classes......Page 693
An inner class can access the outer class members......Page 694
6. Object declarations and expressions......Page 696
...and companion objects......Page 698
Object expressions......Page 699
7. Extensions......Page 700
Using labels with break and continue......Page 703
Using labels with return......Page 704
vararg......Page 705
inline......Page 707
Using Kotlin with JavaScript......Page 708
Writing native code with Kotlin......Page 709
Index......Page 710

✦ Subjects


Computer Science;Programming


📜 SIMILAR VOLUMES


Head First Kotlin: A Brain-Friendly Guid
✍ Dawn Griffiths, David Griffiths 📂 Library 📅 2019 🏛 O'Reilly Media, Inc. 🌐 English

What will you learn from this book? Head First Kotlin is a complete introduction to coding in Kotlin. This hands-on book helps you learn the Kotlin language with a unique method that goes beyond syntax and how-to manuals, and teaches you how to think like a great Kotlin developer. You ll learn every

Head First Kotlin: A Brain-Friendly Guid
✍ Dawn Griffiths, David Griffiths 📂 Library 📅 2019 🏛 O'Reilly Media 🌐 English

What will you learn from this book? Head First Kotlin is a complete introduction to coding in Kotlin. This hands-on book helps you learn the Kotlin language with a unique method that goes beyond syntax and how-to manuals and teaches you how to think like a great Kotlin developer. You’ll learn every

Head first Kotlin: a brain-friendly guid
✍ Griffiths, Dawn;Griffiths, David 📂 Library 📅 2019 🏛 O'Reilly Media 🌐 English

<b>What will you learn from this book?</b><br /><br /><i>Head First Kotlin</i>is a complete introduction to coding in Kotlin. This hands-on book helps you learn the Kotlin language with a unique method that goes beyond syntax and how-to manuals and teaches you how to think like a great Kotlin develo

Head First C: A Brain-Friendly Guide
✍ David Griffiths, Dawn Griffiths 📂 Library 📅 2012 🏛 O'Reilly Media 🌐 English

<div><p>Ever wished you could learn C from a book? <i>Head First C</i> provides a complete learning experience for C and structured imperative programming. With a unique method that goes beyond syntax and how-to manuals, this guide not only teaches you the language, it helps you understand how to be

Head First Python: A Brain-Friendly Guid
✍ Paul Barry 📂 Library 📅 2016 🏛 O'Reilly Media 🌐 English

Want to learn the Python language without slogging your way through how-to manuals? With <i>Head First Python</i>, you'll quickly grasp Python's fundamentals, working with the built-in data structures and functions. Then you'll move on to building your very own webapp, exploring database management,