Head First Kotlin : A Brain-friendly Guide


Head First Kotlin : A Brain-friendly Guide

  • 提携先の海外書籍取次会社に在庫がございます。通常2週間で発送いたします。
    1. 納期遅延や、ご入手不能となる場合が若干ございます。
    2. 複数冊ご注文の場合、分割発送となる場合がございます。
    3. 美品のご指定は承りかねます。
  • 【重要:入荷遅延について】

  • 製本 Paperback:紙装版/ペーパーバック版/ページ数 448 p.
  • 言語 ENG
  • 商品コード 9781491996690
  • DDC分類 005.133

Full Description

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 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 Kotlin uses 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

Your brain on Kotlin. Here you are trying to
learn something, while here your brain is,
doing you a favor by making sure the learning
doesn't stick. Your brain's
thinking, "Better leave room for more
important things, like which wild animals to
avoid and whether naked snowboarding is a bad
idea." So how do you trick your brain
into thinking that your life depends on
knowing how to code in Kotlin?
Who is this book for? xxii
We know what you're thinking xxiii
We know what your brain is thinking xxiii
Metacognition: thinking about thinking xxv
Here's what WE did: xxvi
Read me xxviii
The technical review team xxx
Acknowledgments xxxi
1 Getting Started: A Quick Dip
Kotlin is making waves.
From its first release, Kotlin has impressed
programmers with its friendly syntax,
conciseness, flexibility and power. In this
book, we'll teach you how to build your
own Kotlin applications, and we'll start
by getting you to build a basic application
anc run it. Along the way, you'll be
introduced to some of Kotlin's basic
syntax, such as statements, loops and
conditional branching. Your journey has just
Welcome to Kotlinville 2 (1)
You can use Kotlin nearly everywhere 3 (1)
What we'll do in this chapter 4 (3)
Install IntelliJ IDEA (Community Edition) 7 (1)
Let's build a basic application 8 (3)
You've just created your first Kotlin 11 (1)
Add a new Kotlin file to the project 12 (1)
Anatomy of the main function 13 (1)
Add the main function to App.kt 14 (1)
Test drive 15 (1)
What can you say in the main function? 16 (1)
Loop and loop and loop... 17 (1)
A loopy example 18 (1)
Conditional branching 19 (1)
Using if to return a value 20 (1)
Update the main function 21 (2)
Using the Kotlin interactive shell 23 (1)
You can add multi-line code snippets to the 24 (3)
Mixed Messages 27 (3)
Your Kotlin Toolbox 30 (2)
2 Basic-Types And Variables: Being a Variable
There's one thing all code depends
So in this chapter, we're going to look
under the hood, and show you how Kotlin
variables really work. You'll discover
Kotlin's basic types, such as Ints,
Floats and Booleans, and learn how the Kotlin
compiler can cleverly infer a variable's
type from the value it's given.
You'll find out how to use String
templates to construct complex Strings with
very little code, and you'll learn how
to create arrays to hold multiple values.
Finally, you'll discover why objects are
so important to life in Kotlinville.
Your code needs variables 32 (1)
What happens when you declare a variable 33 (1)
The variable holds a reference to the object 34 (1)
Kotlin's basic types 35 (2)
How to explicitly declare a variable's 37 (1)
Use the right value for the variable's 38 (1)
Assigning a value to another variable 39 (1)
We need to convert the value 40 (1)
What happens when you convert a value 41 (1)
Watch out for overspill 42 (3)
Store multiple values in an array 45 (1)
Create the Phrase-O-Matic application 46 (1)
Add the code to PhraseOMatic.kt 47 (2)
The compiler infers the array's type 49 (1)
from its values
var means the variable can point to a 50 (1)
different array
val means the variable points to the same 51 (3)
array forever...
Mixed References 54 (4)
Your Kotlin Toolbox 58 (2)
3 Functions: Getting Out of Main
It's time to take it up a notch, and
learn about functions. So far, all the code
you've written has been inside your
application's main function. But if you
want to write code that's better
organized and easier to maintain, you need to
know how to split your code into separate
functions. In this chapter, you'll learn
how to write functions and interact with your
application by building a game. You'll
discover how to write compact single
expression functions. Along the way
you'll find out how to iterate through
ranges and collections using the powerful for
Let's build a game: Rock, Paper, 60 (1)
A high-level design of the game 61 (2)
Get the game to choose an option 63 (1)
How you create functions 64 (1)
You can send more than one thing to a 65 (1)
You can get things back from a function 66 (1)
Functions with single-expression bodies 67 (1)
Add the getGameChoice function to Game.kt 68 (7)
The getUserChoice function 75 (1)
How for loops work 76 (2)
Ask the user for their choice 78 (1)
Mixed Messages 79 (2)
We need to validate the user's input 81 (2)
Add the getUserChoice function to Game.kt 83 (4)
Add the printResult function to Game.kt 87 (2)
Your Kotlin Toolbox 89 (3)
4 Classes and Objects: A Bit of Class
It's time we looked beyond Kotlin's
basic types.
Sooner or later, you're going to want to
use something more than Kotlin's basic
types. And that's where classes come in.
Classes are templates that allow you to
create your own types of objects, and define
their properties and functions. Here,
you'll learn how to design and define
classes, and how to use them to create new
types of objects. You'll meet
constructors, initializer blocks, getters and
setters, and you'll discover how they
can be used to protect your properties.
Finally, you'll learn how data hiding is
built into all Kotlin code, saving you time,
effort and a multitude of keystrokes.
Object types are defined using classes 92 (1)
How to design your own classes 93 (1)
Let's define a Dog class 94 (1)
How to create a Dog object 95 (1)
How to access properties and functions 96 (1)
Create a Songs application 97 (1)
The miracle of object creation 98 (1)
How objects are created 99 (1)
Behind the scenes: calling the Dog 100 (5)
Going deeper into properties 105 (1)
Flexible property initialization 106 (1)
How to use initializer blocks 107 (1)
You MUST initialize your properties 108 (3)
How do you validate property values? 111 (1)
How to write a custom getter 112 (1)
How to write a custom setter 113 (2)
The full code for the Dogs project 115 (5)
Your Kotlin Toolbox 120 (2)
5 Subclasses and Superclasses: Using Your
Ever found yourself thinking that an
object's type would be perfect if you
could just change a few things?
Well, that's one of the advantages of
Here, you'll learn how to create
subclasses, and inherit the properties and
functions of a superclass.
You'll discover how to override
functions and properties to make your classes
behave the way you want, and you'll find
out when this is (and isn't) appropriate.
Finally, you'll see how inheritance
helps you avoid duplicate code, and how to
improve your flexibility with polymorphism.
Inheritance helps you avoid duplicate code 122 (1)
What we're going to do 123 (1)
Design an animal class inheritance structure 124 (1)
Use inheritance to avoid duplicate code in 125 (1)
What should the subclasses override? 126 (1)
We can group some of the animals 127 (1)
Add Canine and Feline classes 128 (1)
Use IS-A to test your class hierarchy 129 (1)
The IS-A test works anywhere in the 130 (3)
inheritance tree
We'll create some Kotlin animals 133 (1)
Declare the superclass and its properties 134 (1)
and functions as open
How a subclass inherits from a superclass 135 (1)
How (and when) to override properties 136 (1)
Overriding properties lets you do more than 137 (1)
assign default values
How to override functions 138 (1)
An overridden function or property stays 139 (1)
Add the Hippo class to the Animals project 140 (3)
Add the Canine and Wolf classes 143 (1)
Which function is called? 144 (2)
When you call a function on the variable,
it's the object's version
that responds 146 (1)
You can use a supertype for a 147 (1)
function's parameters and return type
The updated Animals code 148 (5)
Your Kotlin Toolbox 153 (3)
6 Abstract Classes and Interfaces: Serious
A superclass inheritance hierarchy is just
the beginning.
If you want to fully exploit polymorphism,
you need to design using abstract classes and
In this chapter, you'll discover how to
use abstract classes to control which classes
in your hierarchy can and can't be
You'll see how they can force concrete
subclasses to provide their own
You'll find out how to use interfaces to
share behavior between independent classes.
And along the way, you'll learn the ins
and outs of is, as, and when.
The Animal class hierarchy revisited 156 (1)
Some classes shouldn't be instantiated 157 (1)
Abstract or concrete? 158 (1)
An abstract class can have abstract 159 (1)
properties and functions
The Animal class has two abstract functions 160 (2)
How to implement an abstract class 162 (1)
You MUST implement all abstract properties 163 (1)
and functions
Let's update the Animals project 164 (5)
Independent classes can have common behavior 169 (1)
An interface lets you define common
behavior OUTSIDE
a superclass hierarchy 170 (1)
Let's define the Roamable interface 171 (1)
How to define interface properties 172 (1)
Declare that a class implements an 173 (1)
How to implement multiple interfaces 174 (1)
How do you know whether to make a class, a
an abstract class, or an interface? 175 (1)
Update the Animals project 176 (5)
Interfaces let you use polymorphism 181 (1)
Where to use the is operator 182 (1)
Use when to compare a variable against a 183 (1)
bunch of options
The is operator usually performs a smart 184 (1)
Use as to perform an explicit cast 185 (1)
Update the Animals project 186 (3)
Your Kotlin Toolbox 189 (3)
7 Data Classes: Dealing with Data
Nobody wants to spend their life reinventing
the wheel.
Most applications include classes whose main
purpose is to store data, so to make your
coding life easier, the Kotlin developers
came up with the concept of a data class.
Here, you'll learn how data classes
enable you to write code that's cleaner
and more concise than you ever dreamed was
You'll explore the data class utility
functions, and discover how to destructure a
data object into its component parts.
Along the way, you'll find out how
default parameter values can make your code
more flexible, and we'll introduce you
to Any, the mother of all superclasses.
== calls a function named equals 192 (1)
equals is inherited from a superclass named 193 (1)
The common behavior defined by Any 194 (1)
We might want equals to check whether two 195 (1)
objects are equivalent
A data class lets you create data objects 196 (1)
Data classes override their inherited 197 (1)
Copy data objects using the copy function 198 (1)
Data classes define componentN functions... 199 (2)
Create the Recipes project 201 (2)
Mixed Messages 203 (2)
Generated functions only use properties 205 (1)
defined in the constructor
Initializing many properties can lead to 206 (1)
cumbersome code
How to use a constructor's default 207 (3)
Functions can use default values too 210 (1)
Overloading a function 211 (1)
Let's update the Recipes project 212 (1)
The code continued... 213 (4)
Your Kotlin Toolbox 217 (3)
8 Nulls and Exceptions: Safe and Sound
Everybody wants to write code that's
And the great news is that Kotlin was
designed with code-safety at its heart.
We'll start by showing you how
Kotlin's use of nullable types means
that you'll hardly ever experience a
NullPointerException during your entire stay
in Kotlinville.
You'll discover how to make safe calls,
and how Kotlin's Elvis operator stops
you being all shook up.
And when we're done with nulls,
you'll find out how to throw and catch
exceptions like a pro.
How do you remove object references from 220 (1)
Remove an object reference using null 221 (1)
You can use a nullable type everywhere you
can use
a non-nullable type222 (1)
How to create an array of nullable types 223 (1)
How to access a nullable type's 224 (1)
functions and properties
Keep things safe with safe calls 225 (1)
You can chain safe calls together 226 (1)
The story continues... 227 (1)
You can use safe calls to assign values... 228 (3)
Use let to run code if values are not null 231 (1)
Using let with array items 232 (1)
Instead of using an if expression... 233 (1)
The !! operator deliberately throws a 234 (1)
Create the Null Values project 235 (1)
The code continued... 236 (3)
An exception is thrown in exceptional 239 (1)
Catch exceptions using a try/catch 240 (1)
Use finally for the things you want to do 241 (1)
no matter what
An exception is an object of type Exception 242 (2)
You can explicitly throw exceptions 244 (1)
try and throw are both expressions 245 (5)
Your Kotlin Toolbox 250 (2)
9 Collections: Get Organized
Ever wanted something more flexible than an
Kotlin comes with a bunch of useful
collections that give you more flexibility
and greater control over how you store and
manage groups of objects.
Want to keep a resizeable list that you can
keep adding to?
Want to sort, shuffle or reverse its contents?
Want to find something by name?
Or do you want something that will
automatically weed out duplicates without you
lifting a finger?
If you want any of these things, or more,
keep reading.
It's all here...
Arrays can be useful... 252 (1)
...but there are things an array can't 253 (1)
When in doubt, go to the Library 254 (1)
List, Set and Map 255 (1)
Fantastic Lists... 256 (1)
Create a MutableList... 257 (1)
You can remove a value... 258 (1)
You can change the order and make bulk 259 (1)
Create the Collections project 260 (3)
Lists allow duplicate values 263 (1)
How to create a Set 264 (1)
How a Set checks for duplicates 265 (1)
Hash codes and equality 266 (1)
Rules for overriding hashCode and equals 267 (1)
How to use a MutableSet 268 (2)
Update the Collections project 270 (6)
Time fora Map 276 (1)
How to use a Map 277 (1)
Create a MutableMap 278 (1)
You can remove entries from a MutableMap 279 (1)
You can copy Maps and MutableMaps 280 (1)
The full code for the Collections project 281 (4)
Mixed Messages 285 (2)
Your Kotlin Toolbox 287 (3)
10 Generics: Know Your Ins from Your Outs
Everybody likes code that's consistent.
And one way of writing consistent code
that's less prone to problems is to use
In this chapter, we'll look at how
Kotlin's collection classes use generics
to stop you from putting a Cabbage into a
You'll discover when and how to write
your own generic classes, interfaces and
functions, and how to restrict a generic type
to a specific supertype.
Finally, you'll find out how to use
covariance and contravariance, putting YOU in
control of your generic type's behavior.
Collections use generics 290 (1)
How a MutableList is defined 291 (1)
Using type parameters with MutableList 292 (1)
Things you can do with a generic class or 293 (1)
Here's what we're going to do 294 (1)
Create the Pet class hierarchy 295 (1)
Define the Contest class 296 (1)
Add the scores property 297 (1)
Create the getWinners function 298 (1)
Create some Contest objects 299 (2)
Create the Generics project 301 (4)
The Retailer hierarchy 305 (1)
Define the Retailer interface 306 (1)
We can create CatRetailer, DogRetailer and 307 (1)
FishRetailer objects...
Use out to make a generic type covariant 308 (1)
Update the Generics project 309 (4)
We need a Vet class 313 (1)
Create Vet objects 314 (1)
Use in to make a generic type contravariant 315 (1)
A generic type can be locally contravariant 316 (1)
Update the Generics project 317 (7)
Your Kotlin Toolbox 324 (2)
11 Lambdas And Higher-Order Functions: Treating
Code Like Data
Want to write code that's even more
powerful and flexible?
If so, then you need lambdas.
A lambda-or lambda expression-is a block of
code that you can pass around just like an
Here, you'll discover how to define a
lambda, assign it to a variable, and then
execute its code.
You'll learn about function types, and
how these can help you write higher-order
functions that use lambdas for their
parameter or return values.
And along the way, you'll find out how a
little syntactic sugar can make your coding
life sweeter.
Introducing lambdas 326 (1)
What lambda code looks like 327 (1)
You can assign a lambda to a variable 328 (3)
Lambda expressions have a type 331 (1)
The compiler can infer lambda parameter 332 (1)
Use the right lambda for the 333 (1)
variable's type
Create the Lambdas project 334 (5)
You can pass a lambda to a function 339 (1)
Invoke the lambda in the function body 340 (1)
What happens when you call the function 341 (2)
You can move the lambda OUTSIDE the 343 (1)
Update the Lambdas project 344 (3)
A function can return a lambda 347 (1)
Write a function that receives AND returns 348 (1)
How to use the combine function 349 (4)
Use typealias to provide a different name 353 (1)
for an existing type
Update the Lambdas project 354 (7)
Your Kotlin Toolbox 361 (3)
12 Built-In Higher-Order Functions: Power Up
Your Code
Kotlin has an entire host of built-in
higher-order functions.
And in this chapter, we'll introduce you
to some of the most useful ones.
You'll meet the flexible filter family,
and discover how they can help you trim your
collection down to size.
You'll learn how to transform a
collection using map, loop through its items
with forEach, and how to group the items in
your collection using groupBy.
You'll even use fold to perform complex
calculations using just one line of code.
By the end of the chapter, you'll be
able to write code more powerful than you
ever thought possible.
Kotlin has a bunch of built-in higher-order 364 (1)
The min and max functions work with basic 365 (1)
A closer look at minBy and maxBy's 366 (1)
lambda parameter
The sumBy and sumByDouble functions 367 (1)
Create the Groceries project 368 (3)
Meet the filter function 371 (1)
Use map to apply a transform to your 372 (1)
What happens when the code runs 373 (1)
The story continues... 374 (1)
forEach works like a for loop 375 (1)
forEach has no return value 376 (1)
Update the Groceries project 377 (4)
Use groupBy to split your collection into 381 (1)
You can use groupBy in function call chains 382 (1)
How to use the fold function 383 (1)
Behind the scenes: the fold function 384 (2)
Some more examples of fold 386 (1)
Update the Groceries project 387 (4)
Mixed Messages 391 (3)
Your Kotlin Toolbox 394 (1)
Leaving town... 395
i Coroutines: Running Code in Parallel
Some tasks are best performed in the
If you want to read data from a slow external
server, you probably don't want the rest
of your code to hang around, waiting for the
job to complete.
In situations such as these, coroutines are
your new BFF.
Coroutines let you write code that's run
This means less time hanging around, a better
user experience, and it can also make your
application more scalable.
Keep reading, and you'll learn the
secret of how to talk to Bob, while
simultaneously listening to Suzy.