Kotlin Operator

Latest posts
Get notified of the Latest Sport News Update from Our Blog
Share

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.

OperatorsMeaningExpressionTranslate to
+Additionx + yx .plus(y)
Subtractionx – yx.minus(y)
*Multiplicationx * yx.times(y)
/Divisionx / yx.div(y)
%Modulusx % yx.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).

OperatorsMeaningExpressionTranslate to
>greater thanx > yx.compareTo(y) > 0
<less thanx < yx.compareTo(y) < 0
>=greater than or equal tox >= yx.compareTo(y) >= 0
<=less than or equal tox <= y.compareTo(y) <= 0
==is equal tox == y x?.equals(y) ?: (y=== null)
!=not equal tox != 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

OperatorsExpressionSame As
+= a = a + ba+=b
-=a = a – ba-=b
*=a = a * ba*=b
/=a = a / ba/=b
%=a = a % ba%=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

OperatorsExpressionSame As
+  unary plus+aa.unaryPlus()
–  unary minus-aa.unaryMinus()
++ increment by 1++aa.inc()
— decrement by 1–aa.dec()
! not!aa.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

OperatorDescriptionExpressionTranslate 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!aa.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)
}

Related blogs

Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed 

Subscribe to get 15% discount