Learn programming with Kotlin!
The Kotlin Apprentice is a book designed for complete beginners to Kotlin, a modern language used for Android development.
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.
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.
Get a quick-start introduction to Kotlin and how to use Intellij IDEA to work with the projects through this book.
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.
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.
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.
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. These chapters 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.