Kotlin Operators


Operators are used to performing operations on variables and values.

They are various kinds of operators available in Kotlin.

  • Arithmetic operator
  • Relation operator
  • Assignment operator
  • Unary operator
  • Bitwise operation
  • Logical operator

Arithmetic Operator

Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), etc.
OperatorDescriptionExpressionTranslate to
+Additiona+ba.plus(b)
-Subtractiona-ba.minus(b)
*Multiplya*ba.times(b)
/Divisiona/ba.div(b)
%Modulusa%ba.rem(b)

Relation Operator

The relation operator shows the relation and compares between operands. Following are the different relational operators:
OperatorDescriptionExpressionTranslate to
>greater thana>ba.compateTo(b)>0
<Less thana<ba.compateTo(b)<0
>=greater than or equal toa>=ba.compateTo(b)>=0
<=less than or equal toa<=ba?.equals(b)?:(b===null)
==is equal toa==ba?.equals(b)?:(b===null)
!=not equal toa!=b!(a?.equals(b)?:(b===null))


Assignment operator

Assignment operator "=" is used to assign a value to another variable. The assignment of value takes from right to left.
OperatorDescriptionExpressionConvert to
+=add and assigna+=ba.plusAssign(b)
-=subtract and assigna-=ba.minusAssign(b)
*=multiply and assigna*=ba.timesAssign(b)
/=divide and assigna/=ba.divAssign(b)
%=mod and assigna%=ba.remAssign(b)

Unary Operator

A unary operator is used with only a single operand. Following are some unary operators given below.
OperatorDescriptionExpressionConvert to
+unary plus+aa.unaryPlus()
-unary minus-aa.unaryMinus()
++increment by 1++aa.inc()
--decrement by 1--aa.dec()
!not!aa.not()

Logical Operator

Logical operators are used to checking conditions between operands. A list of logical operators is given below.
OperatorDescriptionExpressionConvert to
&&return true if all expression are true(a>b) && (a>c)(a>b) and (a>c)
||return true if any expression are true(a>b) || (a>c)(a>b) or(a>c)
!return complement of expression!aa.not()

Bitwise Operation

In Kotlin, there is not any special bitwise operator. Bitwise operation is done using the named function.
Named FunctionDescriptionExpression
shl (bits)signed shift lefta.shl(b)
shr (bits)signed shift righta.shr(b)
ushr (bits)unsigned shift righta.ushr(b)
and (bits)bitwise anda.and(b)
or (bits)bitwise ora.or(b)
xor (bits)bitwise xora.xor(b)
inv()bitwise inversea.inv()

fun main(){
    //Arithmetic operators (+, -, *, /, %)
    var result = 5+3
    result = result / 2
    // alternatively
    // result /= 2
    result = result * 5
    result = result - 1
    var moduloResult = 5%2
    println( moduloResult)

    //Comparison operators (==, !=, <, >, <=, >=)
    val isEqual = 5==3
    // Concatenation - adding of "Strings"
    println("isEqual is " + isEqual)
    val isNotEqual = 5!=5
    // Kotlin has a feature called String Interpolation.
    // This feature allows you to directly insert a template expression inside a String.
    // Template expressions are tiny pieces of code that are evaluated and
    // their results are concatenated with the original String.
    // A template expression is prefixed with $ symbol.
    // Following are examples of String interpolation
    println("isNotEqual is $isNotEqual")

    println("is5Greater3 ${5 > 3}")
    println("is5LowerEqual3 ${5 >= 3}")
    println("is5LowerEqual5 ${5 >= 5}")

    //Assignment operators (+=, -=, *=, /=, %=)
    var myNum = 5
    myNum += 3
    println("myNum is $myNum")
    myNum *= 4
    println("myNum is $myNum")


    //Increment & Decrement operators (++, --)
    myNum++
    println("myNum is $myNum")
    // increments after use
    println("myNum is ${myNum++}")
    // increments before use
    println("myNum is ${++myNum}")
    println("myNum is ${--myNum}")

}

..

Comments