Operators are symbols that perform mathematical or logical operations. It used to perform operations on values and variables.
Types of Operators in Kotlin
- Arithmetic operator
- Relation operator
- Assignment operator
- Unary operator
- Logical operator
- Bitwise operator
1. Arithmetic Operators – This operators are used to perform mathematical operations.
Operators | Meaning | Expression | Translate to |
+ | Addition | x + y | x .plus(y) |
– | Subtraction | x – y | x.minus(y) |
* | Multiplication | x * y | x.times(y) |
/ | Division | x / y | x.div(y) |
% | Modulus | x % y | x.rem(y) |
fun main()
{
var x = 10
var y = 3
println(x + y) //13
println(x .plus(y))//13
println(x - y)//7
println(x.minus(y))//7
println(x * y) //30
println(x.times(y))//30
println(x / y)//3
println(x.div(y))//3
println(x % y)//1
println(x.rem(y))////1
}
2. Relational Operators – These operators are used to compare two values and return Boolean value(true or false).
Operators | Meaning | Expression | Translate to |
> | greater than | x > y | x.compareTo(y) > 0 |
< | less than | x < y | x.compareTo(y) < 0 |
>= | greater than or equal to | x >= y | x.compareTo(y) >= 0 |
<= | less than or equal to | x <= y | .compareTo(y) <= 0 |
== | is equal to | x == y | x?.equals(y) ?: (y=== null) |
!= | not equal to | x != y | !(x?.equals(y) ?: (y=== null)) > 0 |
compareTo returns zero if this object is equal to the specified other object and returns negative number if it’s less than other, or returns a positive number if it’s greater than other.
fun main()
{
var x = 10
var y = 37
var z = 37
println(x.compareTo(y))//-1
println(y.compareTo(z))//0
println(y.compareTo(x))//1
}
fun main()
{
var x = 10
var y = 37
println(x > y) //false
println(x.compareTo(y)>0)//false
println(x < y)//true
println(x.compareTo(y)<0)//true
println(x >= y) //false
println(x.compareTo(y) >= 0)//false
println(x.compareTo(y) <= 0)//true
println(x == y)//false
println( x?.equals(y))//false
println(x != y)//true
println(!(x.equals(y)))//true
}
3. Assignment Operators – it’s used to assign values to variable
Operators | Expression | Same As |
+= | a = a + b | a+=b |
-= | a = a – b | a-=b |
*= | a = a * b | a*=b |
/= | a = a / b | a/=b |
%= | a = a % b | a%=b |
fun main()
{
var a = 10
var b = 37
a+=b
println(a) // 47
a-=b
println(a) //10
a*=b
println(a)// 370
a/=b
println(a) // 10
a%=b
println(a)//10
}
4. Unary Operators – It uses with only single operand
Operators | Expression | Same As |
+ unary plus | +a | a.unaryPlus() |
– unary minus | -a | a.unaryMinus() |
++ increment by 1 | ++a | a.inc() |
— decrement by 1 | –a | a.dec() |
! not | !a | a.not() |
pre increment operator : ex ++a -It increments the value of variable before using it
Post-increment operator : ex a++ – It increments the value of the variable after executing the expression
fun main()
{
var a = 10
val flag = false
println(-a)// -10
println(a) // 10
println(+a) // 10
println(a.unaryMinus()) // -10
println(a.unaryPlus()) // 10
println(++a)//11
println(a++) // 11 // this is post incriement first incriment then assignn
println(a) // 12
println(--a)// 11
println(a) // 11
println(a--)// 11
println(a) // 10
println(!flag) // true
}
5. Logical Operator : This is used to check condition between operands
Operator | Description | Expression | Translate to |
&& | returns true if both condition are true | (a>b) && (a>c) | (a>b) and (a>c) |
|| | returns true if any expression are true | (a>b) || (a>c) | (a>b) or(a>c) |
! | returns reverse of result | !a | a.not() |
fun main(args : Array<String>){
var a = 50
var b = 20
var c = 5
var flag = false
println((a>b) && (a>c)) // true
println((c>b) || (a>c)) // true
println(!flag)
}