Signup/Sign In

Kotlin Operators

In this tutorial, we will discuss about Kotlin operators. Operators are special characters that are used to carry out operations on one or more operands.

For example:

var num1: Int = 10
var num2: Int = 20
var sum: Int = num1 + num2
println(sum)

In the code example above, num1 and num2 are operands and + is an operator.

There are various type of operators in Kotlin. They are distributed in following categories:

  • Arithmetic operators

  • Comparison operator

  • Assignment operators

  • Equality operators

  • Unary operators

  • Logical operators

  • Miscellaneous Operators

Now, we'll discuss each one of them in detail.

1. Arithmetic operator

Arithmetic operators in Kotlin are used to carry out the basic mathematical operations like addition, subtraction, multiplication, division and modulus.

In the table below we have listed down all the arithmetic operators with basic examples for each.

Operator Description Expression
+ Addition of two variables a + b
- Subtraction of two variables a - b
* Multiplication of two variables a * b
/ Division of two variables a / b
% Modulus of two variables. It will return remainder when a is divided by b. a % b

Let's take a code example:

var num1: Int = 25
var num2: Int = 6

println("Sum: " + (num1 + num2))
println("Difference: " + (num1 - num2))
println("Multiplication: " + (num1 * num2))
println("Division: " + (num1 / num2))
println("Modulus: " + (num1 % num2))


Sum: 31
Difference: 19
Multiplication: 150
Division: 4
Modulus: 1

2. Comparison operator

Comparison operators in Kotlin are used to compare variables. The comparison operators return either true or false, based on the result of the comparison.

Following are the comparison operators supported in Kotlin:

Operator Description Expression
< Checks if one variable is less than other. It returns true if the operand on the left is less than the operand on the right, otherwise returns false. a < b
> Checks if one variable is greater than other. It returns true if the operand on the left is greater than the operand on the right, otherwise returns false. a > b
<= Checks if one variable is less than or equal to other. It returns true if the operand on the left is less than or equal to the operand on the right, otherwise returns false. a <= b
>= Checks if one variable is greater than or equal to other. It returns true if the operand on the left is greater than or equal to the operand on the right, otherwise returns false. a >= b

Let's take a code example:

var num1: Int = 25
var num2: Int = 6

println("Less than: " + (num1 < num2))
println("Greater than: " + (num1 > num2))
println("Less than or equal to: " + (num1 <= num2))
println("Greater than or equal to: " + (num1 >= num2))


Less than: false
Greater than: true
Less than or equal to: false
Greater than or equal to: true

3. Assignment operators

These operators are used to assign value to a variable. We have one simple assignment operator and five augmented assignment operators.

Operator Description Expression
= Assign a value to a variable a = 10
+= Add and assign a value to a variable a+=b
-= Subtract and assign a value to a variable a-=b
*= Multiply and assign a value to a variable a*=b
/= Divide and assign a value to a variable a/=b
%= Mudulus and assign a value to a variable a%=b

Let's take code example to understand the assignment operators:

var num1: Int = 25
var addAndAssign: Int = 0
addAndAssign += num1
println(addAndAssign)

var subAndAssign: Int = 40
subAndAssign -= num1
println(subAndAssign)

var multiplyAndAssign: Int = 1
multiplyAndAssign *= num1
println(multiplyAndAssign)

var divideAndAssign: Int = 50
divideAndAssign /= num1
println(divideAndAssign)

var modulusAndAssign: Int = 30
modulusAndAssign %= num1
println(modulusAndAssign)


25
15
25
2
5

4. Equality operators

These operators are used to check if two variables are equal or not. We have four equality operators.

Operator Description Expression
== Checks if two objects are equal and returns true a == b
!= Checks if two objects are not equal and returns true a != b
=== Checks if two variable refer to same object a === b
!== Checks if two variable does not refer to same object a !== b

The operators === and !== are known as referential equality operators. They check if variables are referring to same object or not.

Let's take an example,

val num1: Int = 25
var num2: Int = 25

println(num1 == num2)
println(num1 != num2)

num2 = num1
println(num1 === num2)
println(num1 !== num2)


true
false
true
false

5. Unary operators

Unary operators apply to only one operand. The below table has all the unary operators:

Operator Description Expression
+ Unary plus, returns positive value +a
- Unary minus, returns negative value -b
++ Increment operator, increase value by 1 a++, ++a
-- Decrement operator, decrease value by 1 b--, --b

Let's take a code example:

var num1: Int = 25
var num2: Int = 10

println("+num1 :"+ +num1)
println("-num2 :"+ -num2)
println("++num1 :"+ ++num1)
println("--num2 :"+ --num1)


+num1 :25
-num2 :-10
++num1 :26
--num2 :25

6. Logical operators

These operators are used to perform logical AND, logical OR and logical NOT operations.

Operator Description Expression
&& AND operator, return true if both inputs are true a && b
|| OR operator, return true if any one is true a || b
! NOT operator, return negation of variable !a

Let's take a code example:

var num1: Int = 25
var num2: Int = -10

var andResult = num1 > 0 && num2 > 0
var orResult = num1 > 0 || num2 > 0
var notResult = !true
println("Check if both are positive: " + andResult)
println("Check if either one is positive: "+ orResult)
println("Not operator on true: "+ notResult)


Check if both are positive: false
Check if either one is positive: true
Not operator on true: false

7. Miscellaneous Operators

Kotlin also supports some more miscellaneous operators. We will discuss some other operators here:

1. Nullable operator (?)

Kotlin doesn't allow a field to be null by default. To make a field nullable, we need to assign ? operator.

var message: String = null       // Error
var nullableMessage: String? = null       // No error

2. The !! operator

The null pointer assertion operator(!!) will convert any value to non-null type and throws an exception if the value is null.

val message: String? = null
println(message!!.length)

This code will not throw any error during compile time but it will throw error at runtime. This operator is not preferred to use.

3. Safe call operator( ?. )

This operator is one of the most important operator in Kotlin. It saves us from null pointer exception. As the name suggests, it helps us to safely call any variable or method of an object by checking if object is null or not.

val message: String? = null
println(message?.length)       // It will return null
println(message!!.length)     // It will through null pointer exception

Here in first case, it will check if message is null. If yes, then instead of calling the method, it will return null.

4. Elvis operator

The Elvis operator in Kotlin is used to assign some other value if the reference is null.

For example:

var message: String? = null
println(message?:"Message is null")       

message = "Hello World"
println(message?:"Message is null")


Message is null
Hello World

Here in first case, when message is null, condition after elvis operator is used and "Message is null" is printed. In second case, when message is not null, message is printed itself.

Summary

In this tutorial we discussed about variuos operators in Kotlin. In the end we also discussed about some operators which will help us in case of null cariable or null pointer exception. In the next tutorial we will discuss about how to take input and show output in kotlin.



About the author:
I'm a writer at studytonight.com.