Operators
Introduction to operators in Kotlin.
Now that we covered variables and data types, lets get into operators. The purpose of an operator is to manipulate a value in terms of another value or itself. Operators are fairly easy to understand because you've likely come across most of them in math classes. Here is a list of some basic operators that you can use.
Arithmetic Operators
These operators are binary, meaning they take two values. For instance, to use the +
operator you would do 5 + 5
. Although mostly just for math, there are other uses for some of these too, which will be discussed later. The type of the result of performing arithmetic operations depends on the types being operated on.
+
- Adds two numbers. Can also combine two strings.-
- Subtracts a number from another.*
- Multiplies two numbers./
- Divides a number by another.%
- Used to get the remainder of a division problem, this is the modulus operator.
Here's an example of how one might use these operators.
val x = "Hello " + "world" // equals "Hello world", same as `val x = "Hello world"
val y = 11 % 2 // equals 1, same as `val y = 1`
val z = 3 * 3 // equals 9, same as `val z = 9`
Comparison Operators
Comparison operators are binary like arithmetic operators, but they return Boolean
values instead of numbers; true
if the statement is true, and false
if it is not. They are used to compare numbers and sometimes other values.
==
- Checks if two values are equal. This works for more than just numbers.!=
- Checks if two values are not equal. This also works for more than numbers.<
- Checks if a number is less than another.>
- Checks if a number is greater than another.<=
- Checks if a number is less than or equal to another.>=
- Checks if a number is greater than or equal to another.
An example of comparison operators:
val x = 6 == 6 // equals true
val y = 4 < 2 // equals false
val z = "foo" != "bar" // equals true
Logical Operators
There are three very useful logical operators. They work with Boolean
values, and return a new Boolean
. Two are binary, but one is unary, meaning it only takes one value. To use a unary operator, you place the operator to the left or right of the value, depending on the operator.
&&
- The and operator. Returnstrue
if the booleans on either side are bothtrue
, andfalse
otherwise.||
- The or operator. Returnstrue
if either one of the booleans aretrue
, andfalse
if they are bothfalse
.!
- The not operator. Returnstrue
if used onfalse
, andfalse
if used ontrue
.
Here are logical operators in action:
val x = true && false // equals false
val y = true || false // equals true
val z = !false // equals true
Your Turn
So much info! We need an exercise! Quick!
Open up
src/main/kotlin/part1/Operators.kt
.Declare two variables containing numbers. They can be whatever you want.
Use
println(...)
to log some operation between these numbers to the console. Try experimentingwith different numbers and different operators.
Now, mix them up. Use comparison operators to get two
Boolean
values from your numbers, andthen a logical operator to operate on the booleans.
Finally, try using
+
on twoString
values, and print the result to the console.
Now, so far we really only know enough to make programs that follow a single course of action no matter what. Now, if that's all that programming could accomplish, we may as well just make a video instead. However, there is another concept in programming called control flow that allows us to make decisions, and this is what we will begin with in part 2. Stay tuned!
Last updated