Introduction
In this part, we are going to learn about operators and comments in Kotlin. Those who are willing to learn about Kotlin, Pros and Cons of it, and Kotlin Variables and DataTypes, please go through my articles on the links below.
Kotlin Operators
Operators are special characters which perform an operation on values or variables.

In Kotlin, there are several kinds of operators, as mentioned below.
- Arithmetic Operator
- Relation Operator
- Assignment Operator
- Unary Operator
- Bitwise Operator
- Logical Operator
Arithmetic Operator
Arithmetic operators are used to perform the basic mathematical operations, such as addition (+), subtraction (-), multiplication (*), division (/) etc.
|
Operator
|
Description
|
Expression
|
|
+
|
Addition
|
a+b
|
|
-
|
Subtraction
|
a-b
|
|
*
|
Multiplication
|
a*b
|
|
/
|
Division
|
a/b
|
|
%
|
Modulus
|
a%b
|
Example
- fun main(args: Array<String>) {
- val number1 = 12.5
- val number2 = 3.5
- var result: Double
- result = number1 + number2
- println("number1 + number2 = $result")
- result = number1 - number2
- println("number1 - number2 = $result")
- result = number1 * number2
- println("number1 * number2 = $result")
- result = number1 / number2
- println("number1 / number2 = $result")
- result = number1 % number2
- println("number1 % number2 = $result")
- }
Output
number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.57
number1 % number2 = 2.0
The +operator is also used for the concatenation of String values.
Example
Concatenation of Strings,
- fun main(args: Array<String>) {
- val start = "Abubackkar Shithik"
- val middle = "Android Developer"
- val end = "Trident Sqa"
- val result = start + middle + end
- println(result)
- }
Output
Abubackkar Shithik Android Developer Trident Sqa
Relation Operator
Relation operator shows the relation and compares between values and variables.
|
Operator
|
Description
|
Expression
|
|
<
|
Less than
|
a<b
|
|
>
|
Greater than
|
a>b
|
|
<=
|
Less than or equal to
|
a<=b
|
|
>=
|
Greater than or equal to
|
a>=b
|
|
Equal ==
|
Is equal to
|
a==b
|
|
!=
|
Not equal to
|
a!=b
|
Example
- fun main(args : Array<String>) {
- val a = 5
- val b = 10
- val result = if (a > b) {
- println("a is greater than b.")
- a
- } else{
- println("b is greater than a.")
- b
- }
- println("result= $result")
- }
Output
b is greater than a
result =10
Assignment operator
Assignment operator "=" is used to assign a value to another variable. The assignment of value takes from right to left.
|
Operator
|
Description
|
Expression
|
|
+=
|
Add and assign
|
a+=b
|
|
-=
|
Sub and assign
|
a-=b
|
|
*=
|
Multiply and assign
|
a*=b
|
|
/=
|
Divide and assign
|
a/=b
|
|
%=
|
Mod and assign
|
a%=b
|

Join the conversation! Your thoughts help the community grow.