In this section, we will learn what the Operators are and how to use them in Kotlin.
What Is an Operator in Kotlin? Symbol in Kotlin
Operator is a “thing” you use to run an operation in Kotlin! For example, let’s say you want to run a mathematical operation like multiplication; the operator for this operation is *
symbol!
Simply put two numbers on the left and right side of this operator and it will return the result of the operation for you.
Another operator in Kotlin is Greater Than operator, which has this symbol >
. Basically, using this operator you can compare two values to see if the value we put on the left side of this operand is greater than the one we put on the right side.
So yes, operator are the things that allow us to run a set of operations in Kotlin!
Kotlin Operands
Operands are those values that we put on the operators to run an operation! For example, the multiplication operator *
needs two values to run and so these two values are called the operands of the multiplication operator.
Example: invoking Kotlin operands
var result = 10 + 20 println(result)
Here the two values 10 and 20 are the operands of the addition operator.
Kotlin LValues and RValues
We have operators that their job is to take one value and assign that to a variable! For example, the assignment =
operator is one example of this type of operators.
When working with assignment operators, the operand you set on the left side of the operator is called LValue
and the operand you put on the right side of the operator is called RValue
.
Be aware that for the assignment operators like =
the LValue must be a variable that points to a memory location!
Basically, assignment operators always assign the value that is on the right side of the operator to the LValue! So this means the LValue must be a placeholder or simply the representation of a memory space that can be used to store a value in it.
So remember: you can’t put a raw value on the left side of the assignment operator! It must be a variable.
On the other hand, RValues can be of any type of value! From a raw value to the value of a variable, to a function call that returns a result (a value) etc.
Basically, the RValues can be any expressions that result a value that could be used to send to a variable (be stored in a memory space).
Example: using lvalues and rvlaues in Kotlin
fun main(){ val num1 = 10 val num2 = 400 val res = num1 + num2 println(res) }
Output:
410
Here, as you can see, there are three Lvalues and all are variables.
The RValues on the other hand, are different for these variables:
– For the num1 variable the RValue is 10.
– For the num2 variable the RValue is 400.
– And for the res variable, the RValue is the result of the expression num1 + num2
which will be 410.
List of Kotlin Operators
There are different categories of operators in Kotlin and each categories’ operators are designed to run specific operations in a certain area!
For example, one of these categories is called Arithmetic Operators where we can find operators that are capable of running mathematical operations like (Multiplication, Addition, Subtraction etc.)
Alright, let’s see these categories and an overall view of the operators that they have.
Note: in later sections, we will get into the greater details of each category.
Kotlin Arithmetic Operators
The Arithmetic Operators are those that we use for mathematical operations. For example, addition +
, multiplication *
, division /
etc. all these and a couple more are part of mathematical operators.
Example: using arithmetic operators in Kotlin
fun main(){ val num1 = 10 val num2 = 400 val mul = num1 * num2 val add = num1 + num2 val divi = 55.0 / 3 println(mul) println(add) println(divi) }
Output:
4000 410 18.333333333333332
Kotlin Comparison Operators
The comparison operators are those that we used to see if two values are equal, or one is greater than the other.
The comparison operators return a boolean value as a result of comparing two values.
Example: using comparison operators in Kotlin
fun main(){ println(10>4) println(400 < 300) println (100 == 100) }
Output:
true false true
Kotlin Logical Operators
Logical operators come handy when working with comparison operators and want to combine the result of multiple comparison to get a final result!
For example, let’s say we want to run a comparison between multiple values and only if all the comparisons resulted the way we wanted, then the final result should be true! This is where the logical operators can help in combining all the results and produce one final output based on those results.
Example: using logical operators in Kotlin
fun main(){ var res = 10 <20 && 40 > 20 println(res) }
Output:
true
As you can see, here we’ve run two comparison operation and then used the AND &&
operator to combine the results of both of these two comparisons and if the result for both of them was true, then the final result would be true as well.
We will get into the logical operators in later sections.
Kotlin Assignment Operators
The assignment operators are used to assign a value to a variable.
One of these assignment operators is the =
. The job of this particular operator is to store a value (we put on the right side of the operator) into a memory space and then make the target variable (the one we set on the left side of the operator) to point to that value.
Example: using assignment operators in Kotlin
fun main(){ var name = "John" var lastName = "Doe" println(name) println(lastName) lastName = "Bauer" println(lastName) }
Output:
John Doe Bauer