Introduction to Kotlin Operators
The following article, provide an outline of the most commonly used operators in Kotlin. Operators are basically special symbols that are used to perform specific operations on the operands. For an example (-) operator is used to perform a subtraction between two operands.
Example:
67-7=60 |
Here 67 and 7 are operands and – is an operator.
Operators in Kotlin
Below are the different types:
1. Arithmetic Operators
Arithmetic Operators are those that are used to perform basic arithmetic calculations like subtraction, addition, multiplication, and division, etc. In Kotlin simple a + b is interpreted as a.plus(b) as a function call.
Kotlin arithmetic operators
Operator | Meaning | Example |
+ | Add two operands | a + b |
– | Subtract right operand from the left operand | a – b |
* | Multiply two operands | a * b |
/ | Divide left operand by 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
}
Note: + operator is also used for String concatenation when used with strings.
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
}
4.8 (8,051 ratings)
View Course
2. Assignment Operators
Assignment Operator is used to assigning values to variables. The value after evaluation on the left-hand side is assigned to the variable of the right-hand side. Besides the basic = assignment operator, Kotlin provides a wide range of assignment Operators which are mentioned below:
Kotlin Assignment Operators
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
Kotlin Unary Operators
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 increment or decrement operator is used before the variable name then the value of that variable is changed first before any other operation on the variable. If the increment or decrement operator is used after a variable name then 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.
Kotlin Comparison Operators
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 result of the comparison. These operators play an important role in the flow of program logic by comparing the values and moving to the flow according to that.
Kotlin equality and inequality Operators
Operators | Meaning | Expression |
!= | Not equal to | a != b |
== | 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.
Kotlin Logical Operators
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.
Kotlin in Operators
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 very useful 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.
Kotlin Range Operators
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 are used to access any value at a particular index in an array. In Kotlin array starts with an index 0.
Kotlin indexed access Operators
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++, Java, Kotlin does not have any bitwise operators. It has various functions that work for bitwise operations.
Kotlin Bitwise Operators
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 have discuss different operators used in kotlin with their examples. You can also go through our other suggested articles to learn more –