Updated July 4, 2023
Introduction to Kotlin Operators
The following article outlines the most commonly used operators in Kotlin. Operators are special symbols used to perform specific operations on the operands. For example, the (-) operator performs a subtraction between two operands.
Example:
67-7=60
Operators in Kotlin, Here 67 and 7 are operands, and – is an operator.
Types of Kotlin Operators
Given below are the different types:
1. Arithmetic Operators
Arithmetic Operators perform basic arithmetic calculations like subtraction, addition, multiplication, division, etc. In Kotlin, simple a + b is interpreted as a.plus(b) as a function call.
Operator | Meaning | Example |
+ | Add two operands | a + b |
– | Subtract the right operand from the left operand | a – b |
* | Multiply two operands | a * b |
/ | Divide the left operand by the right one | a / b |
% | Modulus – returns remainder on dividing two numbers | a % b (deprecated from v 1.1) |
Example:
package com.examples
fun main (args : Array <String>)
{
var num1 = 64
var num2 = 32
val answer : double
answer = num1 +num2
println ("sum = $answer") // sum = 96
answer = num1 - num2
println ("diff = $answer") // diff = 32
answer =num1 * num2
println ("mult = $answer") // mult = 2048
answer = num1 / num2
println ("div = $answer") // div = 2
answer = num1 % num2
println ("mod = $answer") // mod = 0
}
Example:
package com.examples
fun main (args : Array <String>)
{
val fname = "Laxman" val lname = "das"
val full_name = fname + " " + lname println (full_name) // Laxman das
}
2. Assignment Operators
Assignment Operator is used to assign values to variables. The value after evaluation on the left-hand side is assigned to the right-hand side variable.
Besides the basic = assignment operator, Kotlin provides a wide range of assignment Operators, which are mentioned below:
Operator | Example | Equivalent to |
+ = | a+=b | a=a+b |
-= | a-=b | a=a-b |
*= | a*=b | a=a*b |
/= | a/=b | a=a/b |
%= | a%=b | a=a%b |
Example:
package com.examples
fun main (args : Array <String>)
{
var number1 = 22 var number2 = 20 number1 + = 10 number2 % = 3
println ("Result1 = $number1") // Result1 = 32 println ("Result2 = $number2") // Result2 = 2
}
3. Unary Operators
Unary Operators are those that work only on a single operand. Increment ( ++ ) and Decrement ( — ) operators are shortcuts of x = x+1 and x = x – 1
Operators | Example | Equivalent to |
+ | +a | + (a value) |
– | -a | – (a value) |
! | !a | Not a (inversion) |
++ | ++a | a=a+1 |
— | –a | a=a-1 |
Example:
package com.examples
fun main (args : Array <String>)
{
var a = 5 var b = 10 var c = 15
negation = -a increment = ++b dec = c--
println ("Negation = $negation") // Negation = -5 println ("Increment = $increment") // Increment = 11 println (“Decrement = $dec”) // Decrement = 15
}
Increment and Decrement operators can be used before and after a variable, but both have different meanings. If an increment or decrement operator is used before the variable name, then the variable’s value is changed before any other operation on the variable. If the increment or decrement operator is used after a variable name, its value is changed after the other operation on that variable.
In the above example, the value of b is first incremented by 1 and then assigned to variable’ increment,’ whereas the value of c is first assigned to variable dec and then decreases by 1.
4. Comparison Operators
Comparison Operators are used for comparing the two values. These operators are mostly used with if-else for executing specific flow after comparison.
Operator | Meaning | Expression |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than equals to | a >= b |
<= | Less than equals to | a <= b |
Example:
fun main (args : Array <String>)
{
var num1 = 20 var num2 = 30
if (num1 > num2)
{
println ("greater number is $num1")
}
else
{
println ("greater number is $num2") //greater number is 30
}
}
5. Equality and Non-equality Operators
Kotlin provides a set of logical operators, and these equality and non-equality operators return a boolean value depending on the comparison result. These operators play an important role in the program logic flow by comparing the values and moving the flow according to that.
Operators | Meaning | Expression |
!= | Not equal to | a != b |
== | It is equal to | a ==b |
Example:
fun main (args : Array <String>)
{
var a = 3
var b = 6
println (a==b) //false println (a!=b) // true
}
6. Logical Operators
Kotlin provides the below-mentioned 3 logical operators, which return boolean values, either true or false.
Operators | Meaning | Expression |
&& | True if all the values are true | a && b (meaning a and b) |
|| | True if any of the value is true | a || b (meaning a or b) |
! | True if the value is false! | a (meaning not a ) |
Example:
fun main (args : Array <String>)
{
var a = 20 var b = 4 var c = -8
val answer : Boolean
answer = (a>b) || (b<c) println ("answer is " $answer) // answer is true
}
7. In operator
In Kotlin language, In operator is used to check whether a given object is present in the collection or not.
Operators | Meaning | Expression |
in | Is a present in collection b | a in b |
Not in | Is a not present in Collection b | a !in b |
Example:
fun main (args : Array <String>)
{
val array = intArrayOf(10, 20, 30 ,40) If (20 in array)
{
println ("yes 20 is present in array") // yes 20 is present in array
}
else
{
println ("no 20 is not present in array")
}
}
8. Range Operator
Kotlin uses the range operator to create a range of values. This operator is handy when working with loops. It is unnecessary to define every value if it is sequential; it is better to use a shortcut and define the range specifying the lowest and highest value.
Operator | Meaning | Expression |
. . | If i is in the range from a to b | For (i in a . .b) |
Example:
fun main (args : Array <String>)
{
for (i in 1..10)
{
println ("value of i is $i") // value of i is 1
} //value of i is 2 till value of i is 10
}
9. Indexed Access Operator
Indexed Access operators access any value at a particular index in an array. In Kotlin array starts with an index of 0.
Operators | Meaning |
a [i] | Get the value at index’ i’ in an array ‘a’ |
a [i] = b | Set the value b at the ‘i’ index of an array ‘a’ |
Example:
fun main (args : Array <String>)
{
val array = intArrayOf(10, 20, 30, 40, 50) var value = array[1]
println("value at index 1 is $value") //value at index 1 is 20
array[1] = 90
println ("recent value at index 1 is $array[1]") //recent value at index 1 is 90
}
10. Bitwise Operators
Like other programming languages, e.g., C, C++, and Java, Kotlin has no bitwise operators. It has various functions that work for bitwise operations.
Functions | Meaning |
shl | Signed shift left |
shr | Signed shift right |
ushr | Unsigned shift right |
and | Bitwise and |
or | Bitwise or |
xor | Bitwise xor |
inv | Bitwise Inversion |
Example:
fun main (args : Array <String>)
{
var a = 12 var b = 10
val result1 : Int val result2 : Int
result1 = a and b result2 = a or b
println ("final result of and operation is $result1") // final result of and operation is 8
Println ("final result of or operation is $result2") // final result of or operation is 14
}
Recommended Articles
This has been a guide to Kotlin Operators. Here we discuss the introduction, different operators used in Kotlin, and their examples. You can also go through our other suggested articles to learn more –