W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

Kotlin Operators

 In this tutorial you will learn about the Kotlin Operators and its application with practical example.

Kotlin Operators

An operator is a special symbol that is used to carry out some specific operation on its operand. In Kotlin, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Kotlin operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in Kotlin

Kotlin supports the following types of operators –

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Unary Operators
  • Bitwise Operators

Arithmetic Operators In Kotlin

Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc.

Arithmetic operators in Kotlin
OPERATORDESCRIPTIONEXPRESSIONTRANSLATE TO
+Additiona+ba.plus(b)
-Subtractiona-ba.minus(b)
*Multiplya*ba.times(b)
/Divisiona/ba.div(b)
%Modulusa%ba.rem(b)

Example:-

Output:-

Assignment Operators In Kotlin

Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.

Assignment operators in Kotlin
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)

Example:-

Output:-


Comparison (Relational) Operators In Kotlin

Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.

Here is full list of Relational operators available in Kotlin –

Relational operators in Kotlin
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))

Example:-

Output:-

Kotlin Logical Operators

Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.

There are 3 logical operators available in Kotlin.

Logical operators in Kotlin
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()

Example:-

Output:-


Unary Operators In Kotlin

The unary operators operate on a single operand, here is full list of kotlin unary operators.

Unary operators in Kotlin
OPERATORDESCRIPTIONEXPRESSIONCONVERT TO
+unary plus+aa.unaryPlus()
-unary minus-aa.unaryMinus()
++increment by 1++aa.inc()
--decrement by 1--aa.dec()
!not!aa.not()

Example:-

Output:-


Bitwise operators in Kotlin

Bitwise operator are used to perform bit level operation over its operand.

Bitwise operators in Kotlin
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()

Example:-

Output:-


Kotlin in Operator

Kotlin in operator allows you to check whether an object belongs to a collection or not.

Kotlin in Operator
OPERATOREXPRESSIONTRANSLATES TO
ina in bb.contains(a)
!ina !in b!b.contains(a)

Example:-

Output:-


Index access Operator

Index access operator allows you to access elements with specified index in an array or collection.

Kotlin Index access Operator
EXPRESSIONTRANSLATED TO
a[i]a.get(i)
a[i, n]a.get(i, n)
a[i1, i2, ..., in]a.get(i1, i2, ..., in)
a[i] = ba.set(i, b)
a[i, n] = ba.set(i, n, b)
a[i1, i2, ..., in] = ba.set(i1, i2, ..., in, b)

Example:-

Output:-


Kotlin Invoke Operator

Kotlin Invoke Operator
EXPRESSIONTRANSLATED TO
a()a.invoke()
a(i)a.invoke(i)
a(i1, i2, ..., in)a.inkove(i1, i2, ..., in)
a[i] = ba.set(i, b)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.