Kotlin Operators With Examples

In this article, I am going to talk about different types of operators in kotlin language. Kotlin Operators are special symbols to perform specific operations like arithmetic, logical etc.

 

Arithmetic Operators

  1. + : Addition Operator
  2. – :  Subtraction Operator
  3. * : Multiplication Operator
  4. / :  Division Operator
  5. % : Modulus Operator 
val a = 16
val b = 3

val c = a + b
println(c)

val c = a – b
println(c)

val c = a * b
println(c)

val c = a / b
println(c)

val c = a % b
println(c)

 

Basic Assignment Operator

The assignment operator = assigns a value to a variable. 

val month = 11

 

Augmented Assignment Operators

This operators are also called compound assignment operators in other programming languages.

a = a + b

Above can be simplified as below

a += b
 
  1. a = a + b to a += b
  2.  a = a – b to a -= b 
  3. a = a * b to a *= b
  4. a = a / b to a /= b
  5. a = a % b to a %= b

 

Unary Operators

  1. Unary Plus Operator + : It does not change sign of the value. 
  2. Unary Minus Operator – : It changes sign of the value.
  3. Unary Inversion Operator ! : It inverts the value. 
  4. Unary Increment Operator ++ : It can be pre-increment operator mentioned by ++a or post-increment operator mentioned by a++.
  5. Unary Decrement Operator — : It can be pre-decrement operator mentioned by –a or post-decrement operator mentioned by a–.
val a = 16

val c = +a
println(c)

val c = -a
println(c)

val c = !a
println(c)

val c = ++a
println(c)

val c = a–
println(c)

 

Comparison & Equality Operators

  1. >= 
  2. <=  
  3. == 
  4. !=  
val a = 16
val b = 3

if(a > b){
println(“a is greater than b.”) }

if(a >= b){
println(“a is greater than or equal to b.”) }

if(a < b){
println(“a is less than b.”) }

if(a <= b){
println(“a is less than or equal to b.”) }

if(a == b){ println(“a is equal to b.”) }
else if(a != b){ println(“a is not equal to b.”) }

 

Boolean Logical Operators

  1. || : If any of all boolean expressions is true
  2. && : If all boolean expressions are true
val a = 16
val b = 3
val c = 16

if((a == b) || (a == c)){
}

if((a > b) && (a > c)){
}

 

Bitwise Operators

  1. shl : Signed shift left (Equivalent of  << operator)
  2. shr : Signed shift right (Equivalent of  >>  operator)
  3. ushr : Unsigned shift right (Equivalent of  >>> operator)
  4. and : Bitwise and (Equivalent of  operator)
  5. or : Bitwise or (Equivalent of  operator)
  6. xor : Bitwise xor (Equivalent of  operator)
  7. inv : Bitwise inversion (Equivalent of  operator)

 

String Concatenation Operator

For string concatenation, + operator is used.

var name = “Sachin”
var surname = “Tendulkar”
var fullName = name + ” ” + surname

 

Dollar Operator

$ operator is used for adding template expression inside a string.

var name = “Sachin”
var surname = “Tendulkar”
println(“fullname is $name $surname”)

 

Safe Call Operator

Safe call operator ?. is used to call the method only if object is not null.

var name = null
var capName = name?.toUpperCase() // method is not called
var name = “Sachin”
var capName = name?.toUpperCase() // method is called

 

Elvis Operator

Elvis operator ?: returns its first expression if it is not null, otherwise it returns the second expression.

var name = null
var capName = name?.toUpperCase() ?: “abc” // capName is “abc”
var name = “Sachin”
var capName = name?.toUpperCase() ?: “abc” // capName is “SACHIN”

 

Non Null Assertion Operator

This operator !! checks value null or not. If value is null then it throws KotlinNullPointerException.

var name = null
var capName = name!!.toUpperCase() // throw exception
var name = “Sachin”
var capName = name!!.toUpperCase() // No Exception

 

is Operator

is operator is used to check data type of variable at runtime. Its negated form is !is.

int num1 = 16
if(num1 instanceof Int){
println(“type is Int”) }

 

as Operator

as operator is used for to convert one type of variable to another. It is called unsafe cast operator. !as is called safe cast operator.

val obj = “Hello”
val str : String = obj as String
println(str)
 
val obj = 123
val str : String = obj as String
// Throws ClassCastException
 
val obj = 123
val str : String? = obj as? String
println(str) // No Exception

 

in Operator

in operator is used to check whether object belongs to collection. its negated form is !in.

val num = intArrayOf(1, -36, 63, -78)
if (63 in numbers) {
println(“63 is in num array”)
}

 

Double Colon Operator

Double colon operator :: is used to create a class or a function reference.

val intent = Intent(this@MainActivity,SecondActivity::class.java)

 

Range Operator

This operator .. is used to create range of values.

if (63 in 1..100) {
println(“63 is in the range”)
}

 

Index Access Operator

This operator is used to get a obtain a value from an array.

val a = intArrayOf(-2, -1, 0, 1, 2)
println(a[1])
println(a[2])
 
 

Leave a Reply