Do you think IIT Guwahati certified course can help you in your career?
No
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.
Kotlinhas 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.
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));
}
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.