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.
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!
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
Kotlin has a special keyword, object, that makes it easy to follow the
Singleton pattern in your projects, and is used to create properties
specific to a class and not its instances.
You also use the keyword to create anonymous classes.
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 your own classes and classes that were created by someone else.
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.
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.
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.
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.
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.
Spring Ahead Sale — Save on Everything. All videos. All books. Now 50% off.
Build your mobile development skills and save! The mobile development world moves quickly — make 2021 the year you stand out from the rest. Ultimate book & video subscriptions start at just $149/year for as part of our Spring Ahead sale.