Book 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.
Publisher resources
Table of contents
- Authors of Head First Kotlin
- how to use this book: Intro
- Table of Contents (the real thing)
-
1. getting started: A Quick Dip
- Welcome to Kotlinville
- You can use Kotlin nearly everywhere
- What we’ll do in this chapter
- Install IntelliJ IDEA (Community Edition)
- Let’s build a basic application
- You’ve just created your first Kotlin project
- Add a new Kotlin file to the project
- Anatomy of the main function
- Add the main function to App.kt
- What can you say in the main function?
- Loop and loop and loop...
- A loopy example
- Conditional branching
- Using if to return a value
- Update the main function
- Code Magnets
- Using the Kotlin interactive shell
- You can add multi-line code snippets to the REPL
- Code Magnets Solution
- Your Kotlin Toolbox
-
2. basic types and variables: Being a Variable
- Your code needs variables
- What happens when you declare a variable
- The variable holds a reference to the object
- Kotlin’s basic types
- How to explicitly declare a variable’s type
- Use the right value for the variable’s type
- Assigning a value to another variable
- We need to convert the value
- What happens when you convert a value
- Watch out for overspill
- Store multiple values in an array
- Create the Phrase-O-Matic application
- Add the code to PhraseOMatic.kt
- The compiler infers the array’s type from its values
- var means the variable can point to a different array
- val means the variable points to the same array forever...
- Code Magnets
- Code Magnets Solution
- Your Kotlin Toolbox
-
3. functions: Getting Out of Main
- Let’s build a game: Rock, Paper, Scissors
- A high-level design of the game
- Here’s what we’re going to do
- Get the game to choose an option
- How you create functions
- You can send more than one thing to a function
- You can get things back from a function
- Functions with single-expression bodies
- Code Magnets
- Code Magnets Solution
- Add the getGameChoice function to Game.kt
- Behind the scenes: what happens
- The story continues
- The getUserChoice function
- How for loops work
- Ask the user for their choice
- We need to validate the user’s input
- Add the getUserChoice function to Game.kt
- Add the printResult function to Game.kt
- Your Kotlin Toolbox
-
4. classes and objects: A Bit of Class
- Object types are defined using classes
- How to design your own classes
- Let’s define a Dog class
- How to create a Dog object
- How to access properties and functions
- Create a Songs application
- The miracle of object creation
- How objects are created
- Behind the scenes: calling the Dog constructor
- Code Magnets
- Code Magnets Solution
- Going deeper into properties
- Flexible property initialization
- How to use initializer blocks
- You MUST initialize your properties
- How do you validate property values?
- How to write a custom getter
- How to write a custom setter
- The full code for the Dogs project
- Your Kotlin Toolbox
-
5. subclasses and superclasses: Using Your Inheritance
- Inheritance helps you avoid duplicate code
- What we’re going to do
- Design an animal class inheritance structure
- Use inheritance to avoid duplicate code in subclasses
- What should the subclasses override?
- We can group some of the animals
- Add Canine and Feline classes
- Use IS-A to test your class hierarchy
- The IS-A test works anywhere in the inheritance tree
- We’ll create some Kotlin animals
- Declare the superclass and its properties and functions as open
- How a subclass inherits from a superclass
- How (and when) to override properties
- Overriding properties lets you do more than assign default values
- How to override functions
- An overridden function or property stays open...
- Add the Hippo class to the Animals project
- Code Magnets
- Code Magnets Solution
- Add the Canine and Wolf classes
- Which function is called?
- Inheritance guarantees that all subclasses have the functions and properties defined in the superclass
- When you call a function on the variable, it’s the object’s version that responds
- You can use a supertype for a function’s parameters and return type
- The updated Animals code
- Your Kotlin Toolbox
-
6. abstract classes and interfaces: Serious Polymorphism
- The Animal class hierarchy revisited
- Some classes shouldn’t be instantiated
- Abstract or concrete?
- An abstract class can have abstract properties and functions
- The Animal class has two abstract functions
- How to implement an abstract class
- You MUST implement all abstract properties and functions
- Let’s update the Animals project
- Independent classes can have common behavior
- An interface lets you define common behavior OUTSIDE a superclass hierarchy
- Let’s define the Roamable interface
- How to define interface properties
- Declare that a class implements an interface...
- How to implement multiple interfaces
- How do you know whether to make a class, a subclass, an abstract class, or an interface?
- Update the Animals project
- Interfaces let you use polymorphism
- Where to use the is operator
- Use when to compare a variable against a bunch of options
- The is operator usually performs a smart cast
- Use as to perform an explicit cast
- Update the Animals project
- Your Kotlin Toolbox
-
7. data classes: Dealing with Data
- == calls a function named equals
- equals is inherited from a superclass named Any
- The common behavior defined by Any
- We might want equals to check whether two objects are equivalent
- A data class lets you create data objects
- Data classes override their inherited behavior
- Copy data objects using the copy function
- Data classes define componentN functions...
- Create the Recipes project
- Generated functions only use properties defined in the constructor
- Initializing many properties can lead to cumbersome code
- How to use a constructor’s default values
- Functions can use default values too
- Overloading a function
- Let’s update the Recipes project
- The code continued...
- Your Kotlin Toolbox
-
8. nulls and exceptions: Safe and Sound
- How do you remove object references from variables?
- Remove an object reference using null
- You can use a nullable type everywhere you can use a non-nullable type
- How to create an array of nullable types
- How to access a nullable type’s functions and properties
- Keep things safe with safe calls
- You can chain safe calls together
- The story continues
- You can use safe calls to assign values...
- Use let to run code if values are not null
- Using let with array items
- Instead of using an if expression...
- The !! operator deliberately throws a NullPointerException
- Create the Null Values project
- The code continued...
- An exception is thrown in exceptional circumstances
- Catch exceptions using a try/catch
- Use finally for the things you want to do no matter what
- An exception is an object of type Exception
- You can explicitly throw exceptions
- try and throw are both expressions
- Code Magnets
- Code Magnets Solution
- Your Kotlin Toolbox
-
9. collections: Get Organized
- Arrays can be useful...
- ...but there are things an array can’t handle
- When in doubt, go to the Library
- List, Set and Map
- Fantastic Lists...
- Create a MutableList...
- You can remove a value...
- You can change the order and make bulk changes...
- Create the Collections project
- Code Magnets
- Code Magnets Solution
- Lists allow duplicate values
- How to create a Set
- How a Set checks for duplicates
- Hash codes and equality
- Rules for overriding hashCode and equals
- How to use a MutableSet
- You can copy a MutableSet
- Update the Collections project
- Time for a Map
- How to use a Map
- Create a MutableMap
- You can remove entries from a MutableMap
- You can copy Maps and MutableMaps
- The full code for the Collections project
- Your Kotlin Toolbox
-
10. generics: Know Your Ins from Your Outs
- Collections use generics
- How a MutableList is defined
- Using type parameters with MutableList
- Things you can do with a generic class or interface
- Here’s what we’re going to do
- Create the Pet class hierarchy
- Define the Contest class
- Add the scores property
- Create the getWinners function
- Create some Contest objects
- Create the Generics project
- The Retailer hierarchy
- Define the Retailer interface
- We can create CatRetailer, DogRetailer and FishRetailer objects...
- Use out to make a generic type covariant
- Update the Generics project
- We need a Vet class
- Create Vet objects
- Use in to make a generic type contravariant
- A generic type can be locally contravariant
- Update the Generics project
- Your Kotlin Toolbox
-
11. lambdas and higher-order functions: Treating Code Like Data
- Introducing lambdas
- What lambda code looks like
- You can assign a lambda to a variable
- What happens when you invoke a lambda
- Lambda expressions have a type
- The compiler can infer lambda parameter types
- Use the right lambda for the variable’s type
- Create the Lambdas project
- You can pass a lambda to a function
- Invoke the lambda in the function body
- What happens when you call the function
- You can move the lambda OUTSIDE the ()’s...
- Update the Lambdas project
- A function can return a lambda
- Write a function that receives AND returns lambdas
- How to use the combine function
- Use typealias to provide a different name for an existing type
- Update the Lambdas project
- Code Magnets
- Code Magnets Solution
- Your Kotlin Toolbox
-
12. built-in higher-order functions: Power Up Your Code
- Kotlin has a bunch of built-in higher-order functions
- The min and max functions work with basic types
- A closer look at minBy and maxBy’s lambda parameter
- The sumBy and sumByDouble functions
- Create the Groceries project
- Meet the filter function
- Use map to apply a transform to your collection
- What happens when the code runs
- The story continues...
- forEach works like a for loop
- forEach has no return value
- Update the Groceries project
- Use groupBy to split your collection into groups
- You can use groupBy in function call chains
- How to use the fold function
- Behind the scenes: the fold function
- Some more examples of fold
- Update the Groceries project
- Your Kotlin Toolbox
- Leaving town...
- A. coroutines: Running Code in Parallel
- B. testing: Hold Your Code to Account
-
C. leftovers: The Top Ten Things: (We Didn’t Cover)
- 1. Packages and imports
- The fully qualified name
- 2. Visibility modifiers
- Visibility modifiers and classes/interfaces
- 3. Enum classes
- enum properties and functions
- 4. Sealed classes
- How to use sealed classes
- 5. Nested and inner classes
- An inner class can access the outer class members
- 6. Object declarations and expressions
- Class objects...
- Object expressions
- 7. Extensions
- 8. Return, break and continue
- Using labels with return
- 9. More fun with functions
- 10. Interoperability
- Index
Product information
- Title: Head First Kotlin
- Author(s):
- Release date: February 2019
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781491996690
You might also like
book
Kotlin in Action
Kotlin in Action guides experienced Java developers from the language basics of Kotlin all the way …
video
Introduction to Kotlin Programming
Kotlin 1.0 was released in February 2016, and since that time it’s been embraced by developers …
book
Programming Kotlin
Programmers don't just use Kotlin, they love it. Even Google has adopted it as a first-class …
video
Advanced Kotlin Programming
Designed for developers who already have a basic understanding of Kotlin, this video course examines some …