In this section, we will learn what the Assignment operators are and how to use them in Kotlin.
Note: we’re assuming you’re already familiar with the Kotlin Operators in General.
What Is Assignment Operator in Kotlin?
The assignment operators are those types of operators that we use to assign a value to a variable.
For example, the =
operator is one example of the assignment operators where it takes one value (on its right side) and store that into a variable (that we set on the left side of this operator).
List of assignment operators in Kotlin
In the table below, you can see the list of assignment operators supported in Kotlin.
Operator | Symbol |
Assignment operator | = |
Addition and Assignment operator | += |
Subtract and Assignment operator | -= |
Multiplication and Assignment operator | *= |
Division and Assignment operator | /= |
Kotlin Assignment =
Operator:
The assignment operator =
is used to assign a value to a variable. Note that using this operator, the value we set on its right side will be first stored into a memory space and then the address of that location is returned and stored in the variable that we set on the left side of this operator.
Assignment =
Operator Syntax:
LValue = RValue
Note: please check the Kotlin and Operators in General section if you’re not familiar with Lvalues and RValues.
Example: using assignment operator in Kotlin
fun main(){ var firstName = "Omid" var lastName = "Dehghan" println(firstName) println(lastName) }
Output:
Omid Dehghan
Kotlin Addition Assignment +=
Operator
The Addition Assignment operator is a combination of both addition and the assignment operator.
Here’s how it works:
We set a variable on the left side of this operator and a value on the right side (which could be the value of a variable or the result of calling a function, etc.)
Now this operator will take the value on the right side and add it to the current value of the variable on the left side and then store the result into the variable that we set on the left side of this operator.
Addition Assignment +=
Operator Syntax:
LValue += RValue
Example: using addition assignment operator in Kotlin
fun main(){ var salary = 100 salary += 200 println(salary) }
Output:
300
Kotlin Subtract Assignment -=
Operator
The subtract assignment operator is a combination of subtract and the assignment operators.
It is used to subtract the current value of a variable from a value (that we set on the right side of the operator) and then store the result in the variable itself.
Subtract Assignment -=
Operator Syntax:
LValue -= RValue
Example: using subtract assignment operator in Kotlin
fun main(){ var salary = 500 salary -= 200 println(salary) }
Output:
300
Kotlin Multiplication Assignment *=
Operator
The multiplication and assignment operator is the one we use when we want to multiply the value of a variable to another value and then store the result in the variable itself.
Multiplication Assignment *=
Operator Syntax:
LValue *= RValue
Example: using multiplication assignment operator in Kotlin
fun main(){ var salary = 500 salary *= 3 println(salary) }
Output:
1500
Kotlin Division Assignment /=
Operator
The division and assignment operator is used when we want to divide the value of a variable by another one and then assign the result into the variable itself.
Division Assignment /=
Operator Syntax:
LValue /= RValue
Example: using division assignment operator in Kotlin
fun main(){ var salary = 100 salary /= 2 println(salary) }
Output:
50