Table of Contents (the real thing)
-
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?
-
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 and 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 begun...
-
There’s one thing all code depends on—variables.
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.
-
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 loop.
-
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.
-
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 inheritance. 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.
-
A superclass inheritance hierarchy is just the beginning.If you want to fully exploit polymorphism, you need to design using abstract classes and interfaces. In this chapter, you’ll discover how to use abstract classes to control which classes in your hierarchy can and can’t be instantiated. You’ll see how they can force concrete subclasses to provide their own implementations. 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.
-
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 possible. 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.
-
Everybody wants to write code that’s safe.
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.
-
Ever wanted something more flexible than an array?
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...
-
Everybody likes code that’s consistent.
And one way of writing consistent code that’s less prone to problems is to use generics. In this chapter, we’ll look at how Kotlin’s collection classes use generics to stop you from putting a Cabbage into a List<Seagull>. 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.
-
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 object. 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.
-
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.
-
Some tasks are best performed in the background.
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 asynchronously. 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.
-
Everybody knows that good code needs to work.
But each code change that you make runs the risk of introducing fresh bugs that stop your code from working as it should. That’s why thorough testing is so important: it means you get to know about any problems in your code before it’s deployed to the live environment. In this appendix, we’ll discuss JUnit and KotlinTest, two libraries which you can use to unit test your code so that you always have a safety net.
-
Even after all that, there’s still a little more.
There are just a few more things we think you need to know. We wouldn’t feel right about ignoring them, and we really wanted to give you a book you’d be able to lift without training at the local gym. Before you put down the book, read through these tidbits.
Get Head First Kotlin now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.