Android Programming with Kotlin for Beginners
上QQ阅读APP看书,第一时间看更新

Chapter 7. Kotlin Variables, Operators, and Expressions

In this chapter and the next, we are going to learn and practice the core fundamentals of Kotlin. In fact, we will explore the main principles of programming in general. In this chapter, we will focus on the creation and understanding of data itself, and in the next chapter, we will explore how to manipulate and respond to it.

This chapter will focus on the simplest type of data in Kotlin – variables. We will revisit more complex and powerful types of data in Chapter 15, Handling Data and Generating Random Numbers.

The core Kotlin fundamentals that we'll learn about apply when working within classes that we inherit from (such as Activity and AppCompatActivity) and the classes that we write ourselves (as we will start to do in Chapter 10, Object-Oriented Programming).

As it is more logical to learn the basics before we write our own classes, we will learn the basics and then use the extended Activity class, AppCompatActivity, to put this new theory into practice. We will use Log and Toast again to see the results of our coding. In addition, we will use more functions that we will write ourselves (called from buttons) as well as the overridden functions of the Activity class to trigger the execution of our code. We will save studying the full details on functions, however, until Chapter 9, Kotlin Functions.

When we move onto Chapter 10, Object-Oriented Programming, and start to write our own classes, as well as gain an understanding about how classes that are written by others work, everything we have learned here will still apply then too.

By the end of the chapter, you will be comfortable writing Kotlin code that creates and uses data within Android. This chapter will take you through the following topics:

  • Learning the jargon
  • Learning some more about code comments
  • What are variables?
  • Types of variables
  • The different ways to declare variables
  • Initializing variables
  • Operators and expressions
  • The express yourself demo app

Let's start by finding out exactly what a variable is.