Version Second Edition
Platform Android 10
Language Kotlin 1.3
Editor IDEA

For complete beginners to Kotlin. No prior programming experience necessary!

This is a book for complete beginners to the new, modern Kotlin language.

Everything in the book takes place in a clean, modern development environment, which means you can focus on the core features of programming in the Kotlin language, without getting bogged down in the many details of building apps.

This is a sister book to the Android Apprentice the Android Apprentice focuses on making apps for Android, while the Kotlin Apprentice focuses on the Kotlin language fundamentals.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

Section 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, then you have a chance to do things with your newly learned data types. Finally, you’ll move past the variables and constants you’ve been working with, which have had concrete values, to more tricky data types.

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!

Get a quick-start introduction to Kotlin and how to use Intellij IDEA to work with the projects through this book.

1

This is it, your whirlwind introduction to the world of programming! You’ll begin with an overview of computers and programming, and then spend your time working with code comments, arithmetic operations, constants and variables.

2

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.

3

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.

4

Continuing the theme of code not running in a straight line, you’ll learn about another loop known as the `for` loop. You’ll also learn about when expressions which are particularly powerful in Kotlin.

5

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.

6

Many programming languages suffer from the "billion dollar mistake" of null values. You'll learn about how Kotlin protects you from the dreaded null pointer exception.

7

Section II: Collections & Lambdas

So far, you’ve mostly seen data in the form of single elements. Although pairs and triples can have multiple pieces of data, you’re limited to combining only two or three items with them. In this section, you’ll learn about collection types in Kotlin. Collections are flexible “containers” that let you store any number of values together.

The chapters in this section will give you the foundation that you need to organize your data and code so that you can begin to do more sophisticated programming in Kotlin!

Arrays and lists are the most common collection types you’ll run into in Kotlin. They both are typed like regular variables and constants, and store multiple values in a single data structure. See how to iterate through arrays and lists and how to modify their contents.

8

Maps are useful when you want to look up values by means of an identifier. For example, the table of contents of this book maps chapter names to their page numbers, making it easy to skip to the chapter you want to read. With an array, you can only fetch a value by its index, which has to be an integer, and all indexes have to be sequential. In a map, the keys can be of any type and are generally in no particular order. You'll also learn about Sets, which let you store unique values in a collection.

9

A previous chapter taught you about functions. But Kotlin has another construct you can use to break up code into reusable chunks: lambdas. These have many uses, and become particularly useful when dealing with collections such as a list or map.

10

Section III: Building Your Own Types

You can create your own type by combining variables and functions into a new type definition. For example, integers and doubles might not be enough for your purposes, and you might need to create a type to store complex numbers. Or maybe storing first, middle and last names in three independent variables is getting difficult to manage, so you decide to create a FullName type.

When you create a new type, you give it a name; thus, these custom types are known as named types. Named types are a powerful tool for modeling real-world concepts. You can encapsulate related concepts, properties and methods into a single, cohesive model.

Custom types make it possible to build large and complex things with the basic building blocks that you’ve learned so far. It’s time to take your Kotlin apprenticeship to the next level!

In this chapter, you’ll get acquainted with classes, which are are named types. Classes are one of the cornerstones of object-oriented programming, a style of programming where the types have both data and behavior. In classes, data takes the form of properties and behavior is implemented using functions called methods.

11

Kotlin has a special keyword object that makes it easy to follow the Singleton pattern in your projects, and that is also used to create properties specific to a class and not its instances. You also use the keyword to create anonymouse classes.

12

In this chapter, you’ll learn more about Kotlin properties, along with some tricks to deal with properties, how to monitor changes in a property’s value and how to delay initialization of a property.

13

Methods are merely functions that reside in a class. In this chapter, you’ll take a closer look at methods and see how to add methods onto classes that were created by someone else.

14

Having seen the basics of creating classes, in this chapter you'll see the more advanced aspects of object-oriented programing, including inheritance and limiting member visibility.

15

Enumerations are useful when you have a quantity that can take on a finite set of discrete values. See how to define and use enum classes and see some examples of working with enum classes and when expressions.

16

Classes are used when you want to create types that contain both state and behavior. When you need a type that allows primarily the specification of behavior, you're better off using an interface. See how to create and use interfaces.

17

At some point you will need to ability to create abstractions that go beyond what's available in regular classes and functions. You'll learn how to use generics to super-power your classes and functions.

18

Section IV: Intermediate Topics

You’ve made it to the final section of this book! In this section, you’ll delve into some important but more intermediate topics to round out your Kotlin apprenticeship.

Kotlin was designed from the start to be 100% compatible with Java and the Java ecosystem. See how to work with Kotlin and Java together in a single project and how to call back and forth between the two.

19

No software is immune to error conditions. See how to use exceptions in Kotlin to provide some control over when and how errors are handled.

20

Kotlin goes beyond just being an object-oriented programming language, and provides many of the constructs found in the domain of functional programming. See how to treat functions as first-class citizens by learning how to use functions as parameters and return values from other functions.

21

You'll learn the concept of conventions and see how to use conventions to implement operator overloading and write more concise but still readable code.

22

Kotlin coroutines allow you to simplify your asynchrounous code and make it much more readable. You'll learn the difference between threads and coroutines and see examples of coroutines in use.

23

Kotlin is not just useful on Android or the JVM, but also can be used for scripting at the command line. See how to use Kotlin as a scripting language.

24

Install and become familiar with the Kotlin/Native tools that compile Kotlin code to native binaries for iOS and other platforms.

25

By creating a project with a shared Kotlin module, you can share Kotlin code between multiple platforms. See how to create a Multiplatform project for iOS and Android, and use the expect and actual keywords for platform-specific behavior.

26

Appendices

Meet the team

Contributors

Who is this book for

For complete beginners to Kotlin. No prior programming experience necessary!

Concepts covered in this book

  • Kotlin Development Environment
  • Numbers and Strings
  • Making Decisions
  • Functions and Lambdas
  • Collection Types
  • Building Your Own Types
  • Functional Programming
  • Coroutines
  • Kotlin Platforms & Scripting
  • Kotlin/Native & Multiplatform

Version history

Second Edition · Android 10, Kotlin 1.3, IDEA

v2.0 · Sep 30 2019 · Eli Ganim, Irina Galata, Cosmin Pupăză, Ellen Shapiro, Matt Galloway & Ben Morrow

Kotlin Apprentice

By Eli Ganim, Irina Galata, Cosmin Pupăză, Ellen Shapiro, Matt Galloway and Ben Morrow

Learn programming with Kotlin! The Kotlin Apprentice is a book designed for complete beginners to Kotlin, a modern language used for Android development.

Read for Free with an Ultimate Beginner Subscription* * Includes this and all other Beginner books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

All videos. All books.
One low price.

A raywenderlich.com subscription is the best way to learn and master mobile development — plans start at just $19.99/month!

Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.