In this lesson, you will learn how to store data, and manipulate data, through the use of variables and operators in Kotlin.
A variable is a way of storing information. It is made up of three things: a name, a type, and a value.
In Kotlin, variables can be defines as "immutable" (can't be changed after being created) or "mutable" (can be changed after being created). An immutable variable is declared with val, while a mutable variable is declared with var
One of the three parts that make up a variable is the "data type." The type of a variable tells you what kind of data it stores, the most basic types are called "primitives", and the main important ones for us can be seen in the table below
Int, used for storing whole numbers.
Float and Double, used for storing numbers with decimal values.
Char, used for storing a single character, like a letter, number, or symbol.
Boolean, can be either true or false
There are also some other basic types that are important to know, that aren't truly "primitive" types, which can be seen below.
String, used for storing things like words, it's a list of characters (see Char)
In Kotlin, by default most values can not be set to null by default. Null is the absence of a value, and if you want to have a "nullable" variable, when declaring the type, you will add a question mark after the type. i.e. Int?, String?, and Double?
Now that we know all of that, we know what we need to write down to define a variable right? We need to say if it's mutable or immutable, give it a type, give it a name, and give it a value (which could be null, if we make our variable nullable). The syntax for this is pretty simple: val name: DataType = value. You start with either val or var to declare if your variable is immutable, or mutable, then you give it a name, then a data type, then use the assignment operator = to give it a value.
"Operators" are symbols in your code that change and interact with variables. Kotlin has several different kinds of operators, including arithmetic, assignment, comparison, and logical operators.
Arithmetic operators are used to perform mathematical operations on variables and values, the following table lists and explains each operator.
+: Addition
-: Subtraction
*: Multiplication
/: Division
%: Modulus (Remainder)
Assignment operators are used to give a new variable a value, and change the value of mutable variables. There are several assignment operators, most of which are used for doing assignment and arithmetic at the same time
=: Sets the value of the variable on the left, to the expression on the right
+=: Sets the variable on the left to itself PLUS the expression on the right (i.e. x += 5 is the same as x = x + 5)
-=: Sets the variable on the left to itself MINUS the expression on the right
/=: Sets the variable on the left to itself DIVIDED BY the expression on the right
*=: Sets the variable on the left to itself MULTIPLIED BY the expression on the right
Comparison operators are used to compare two values. Comparison operators always return a boolean (true/false) value. The table below explains the main important ones.