Table of contents
1.
Introduction 
2.
Operators in Kotlin
2.1.
Unary Operator
2.2.
Binary Operator
3.
Arithmetic Operators in Kotlin
4.
Comparison Operators in Kotlin
5.
Logical Operators in Kotlin
6.
Assignment Operators in Kotlin
7.
Miscellaneous Operators in Kotlin
7.1.
Safe call operator( ?. )
7.2.
Nullable operator (?)
7.3.
Elvis operator
7.4.
Null pointer assertion operator (!!)
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Operators in Kotlin

Author Hari Sapna Nair
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Operators play an important role in any programming language. Operators are used to building applications like a calculator application, where mathematical calculations are required. They also perform logical comparisons and execute a required task in a software application.

This blog will discuss Operators in Kotlin like arithmetic operators, comparison operators, logical operators, etc., in detail.

Operators in Kotlin

Operators are special characters used for performing operations on variables and values. The value is called the operand on which the operator is applied. For instance, in "4 - 3", 4 and 3 are the operands, and "-" is the operator.

Kotlin has various operators to perform assignment, arithmetic, comparison operators, etc. We will explore these operators in detail in this blog.

Operators in Kotlin can be classified into two types based on the number of operands used. It can be classified into two: unary and binary operator.

Unary Operator

It is an operator with a single operand.

Example

fun main() {
 var a = 10;
 println("a before updation: " + a);
 // Unary operator
 a++;
 println("a after updation: " + a);
}

 

Output

a before updation: 10
a after updation: 11

Binary Operator

It is an operator with two operands.

Example

fun main() {
 var a = 10;
 var b = 20;
 // Binary operator
 var c = a + b;
 println(c);
}

 

Output

30

Arithmetic Operators in Kotlin

The arithmetic operator performs arithmetic operations on the operands. Let's see the list of arithmetic operators in Kotlin.

Operator

Description

+

Used to perform addition

-

Used to perform subtraction

*

Used to perform multiplication

/

Used to perform division

%

Used to obtain the remainder

++

Used to increment the value by 1

--

Used to decrement the value by 1

 

Example

fun main() {
 var num1 = 10;
 var num2 = 5;
 // Addition
 println("num1 + num2: " + (num1 + num2));

 // Subtraction
 println("num1 - num2: " + (num1 - num2));

  // Multiplication
 println("num1 * num2: " + (num1 * num2));

  // Division
 println("num1 / num2: " + (num1 / num2));

  // Remainder
 println("num1 % num2: " + (num1 % num2));

  // Increment
 println("num1++: " + num1++);

  // Decrement
 println("num1--: " + num1--);
}

 

Output

num1 + num2: 15
num1 - num2: 5
num1 * num2: 50
num1 / num2: 2
num1 % num2: 0
num1++: 10
num1--: 11

 

How do arithmetic operators work under the hood?

Consider using an arithmetic operator to subtract two numbers, a and b. Under the hood, the expression a - b calls a.minus(b) member function. Let's look at the arithmetic operators and their corresponding functions.

Expression

Translates to

a + b a.plus(b)
a - b a.minus(b)
a * b a.times(b)
a / b a.div(b)
a % b a.mod(b)

Comparison Operators in Kotlin

The comparison operator compares the two operands. The result is always true or false in this case. Let's go through the various comparison operators in Kotlin.

Operator

Description

== 

(equal to)

It returns true when the operands are equal.

!=

(not equal to)

It returns true when the operands are not equal.

>

(greater than)

It returns true when the value of the left operand is greater than the value of the right operand.

>=

(greater than or equal to)

It returns true when the value of the left operand is greater than or equal to the value of the right operand.

<

(Lesser than)

It returns true when the value of the left operand is lesser than the value of the right operand.

<=

(Lesser than or equal to)

It returns true when the value of the left operand is lesser than or equal to the value of the right operand.

 

Example

fun main() {
 var val1 = 10;
 var val2 = 5;
 var val3 = 10;
 var val4 = 15;
  // Equal to operator
  println("val1 == val3: " + (val1 == val3));

  // Not equal to operator
  println("val1 != val2: " + (val1 != val2));

  // greater than operator
  println("val1 > val2: " + (val1 > val2));

  // greater than or equal operator
  println("val1 >= val4: " + (val1 >= val4));

  // lesser than operator
  println("val1 < val4: " + (val1 < val4));

  // lesser than or equal operator
  println("val1 <= val2: " + (val1 <= val2));
}

 

Output

val1 == val3: true
val1 != val2: true
val1 > val2: true
val1 >= val4: false
val1 < val4: true
val1 <= val2: false

 

Read about Bitwise Operators in C here.

Know more about Unary operator overloading in c++ in detail here.

Logical Operators in Kotlin

Logical operations like NOT, OR, and AND are performed using logical operators. Let's go through the various logical operators in Kotlin.

Operator

Description

&&

(Logical AND)

It returns the value true when both the operands/boolean values are true. Else false is returned.

||

(Logical OR)

It returns the value true when at least one of the operands/boolean values are true. Else false is returned.

!

(Logical NOT)

It returns the value true when the operand is false and vice-versa.

 

Example

fun main() {
 var val1 = true;
 var val2 = false;
  // Logical and operator
  println("val1 && val2: "+ (val1 && val2));

  // Logical or operator
  println("val1 || val2: "+ (val1 || val2));

  // Logical not operator
  println("!val1: "+ (!val1));
}

 

Output

val1 && val2: false
val1 || val2: true
!val1: false

Assignment Operators in Kotlin

Assignment operators assign values to the operand. Let's go through the various assignment operators in Kotlin.

 

Operator

Description

=

It assigns values from the right operator to the left operator.

+=

It adds the right operator to the left operator and assigns the resultant value to the left operand.

-=

It subtracts the right operand from the left operand and assigns the resultant value to the left operand.

*=

It multiplies the right operand with the left operand and assigns the resultant value to the left operand.

/=

It divides the left operand with the right operand and assigns the resultant value to the left operand.

%=

It takes the modulus using two operands and assigns the result to the left operand.


Example

fun main() {
   // Assignment operator
   var num1 = 5;
  
   // Addition assignment operator
   num1 += 2;
   println("num1: "+ num1);

   // Subtraction assignment operator
   num1 -= 8;
   println("num1: "+ num1);

   // Multiplication assignment operator
   num1 *= 2;
   println("num1: "+ num1);

   // Division assignment operator
   num1 /= 5;
   println("num1: "+ num1);
}

 

Output

num1: 7
num1: -1
num1: -2
num1: 0

Miscellaneous Operators in Kotlin

Kotlin supports some miscellaneous operators like safe call operator, nullable operator, Elvis operator, etc. Let's look at them in detail.

Safe call operator( ?. )

The safe call operator saves us from the null pointer exceptions. It helps us safely call any variable or method of an object by checking if the object is null or not.

Example

fun main() {
   val name: String? = null
   // Returns null
   println(name?.length)  
}

 

Output

null

Nullable operator (?)

To make a field nullable in Kotlin, we need to assign the nullable operator. This is because Kotlin doesn't allow a field to be null by default. 
 

Example

fun main() {
   // Error
   var name: String = null       
}

 

In this case, the following error is obtained.

Error
 

This can be solved using the nullable operator. 

fun main() {
   // No error
   var name: String? = null         
}

Elvis operator

The Elvis operator in Kotlin assigns some other value if the reference is null. Let's see an example.

 

Example

fun main() {
   var name: String? = null
   println(name?:"name is null")      
  
   name = "Sapna"
   println(name?:"name is null")      
}

 

Output

name is null
Sapna

 

When the name is null, the condition after Elvis operator is used and "name is null" is printed in the first case. In the second case, when the name is not null, the name is printed itself.

Null pointer assertion operator (!!)

The null pointer assertion operator converts any value to a non-null type, and it throws an exception if the value is null.

 

Example

fun main() {
   var name: String? = null
   println(name!!.length)
}

 

Error

FAQs

1. Do we have a ternary operator in Kotlin?

Ans: There is no ternary conditional operator in Kotlin. However, a similar result can be obtained using if-else and when statements.

 

2. Where are comparison and equality operators commonly used in Kotlin?

Ans: Comparison and equality operators are used in control flow like if expression, when expression, and loops.

 

3. How are bitwise operations performed in Kotlin?

Ans: Unlike other programming languages, there are no bitwise operators in Kotlin. To perform the bitwise operations, various functions are used instead. Some of the functions are:-

  • or - Bitwise or
  • shr - Signed shift right
  • ushr - Unsigned shift right
  • inv - Bitwise inversion
  • xor - Bitwise xor

 

4. What is meant by referential equality operators and structural equality operators?

Ans: The operators like === and !== are called as referential equality operators. They check if variables are referring to the same object or not.

The operators like == and != are called structural equality operators. They check if two variables contain equal data.

 

5. Why is the null pointer assertion operator not preferred to use?

Ans: The null pointer assertion operator is not preferred because it does not throw any error during compile time but throws an error at runtime. 
 

6. What is the use of the in operator in Kotlin?

Ans: The in operator in Kotlin checks whether an object belongs to a collection.

Example

fun main() {
   val arr = intArrayOf(1, 2, 3, 4);
   println(2 in arr);
   println(2 !in arr);
}

Output

true
false

Key Takeaways

The blog covered the various Operators in Kotlin, namely, arithmetic operators, comparison operators, logical operators, assignment operators, and some miscellaneous operators like safe call operator, nullable operator, Elvis operator, etc
To learn more about Micro Operations, refer to Arithmetic Micro Operations.

Don't stop here. Check out our blogs to learn Kotlin. Feel free to refer to the blogs Kotlin and its resourceful librariesKotlin Architecture and Environment SetupKotlin Null Safety, etc.

We hope you found this blog useful. Liked the blog? Then feel free to upvote and share it.

Live masterclass