Kotlin Programming – Basic Operators

In this tutorial you’ll learn about basic operators in Kotlin. To follow along, you can make use of the Kotlin – Playground.

Basic Operators

Operators are special symbols or phrases that are used to check, change or combine values. This tutorial explains some common operators of Kotlin such as the addition operator (+), the lazy conjunction operator (&&), the assignment operator (=), equal to operator (==) and arithmetic operators (+,,*,/).

Terminology

– Unary operators are applied on a single target such as (-x). There are two types of such operators, Unary prefix operators appears before their target such as (-x) and Unary postfix operators appears after their target such as (y!).

– Binary operators are applied in between two targets such as (x + y).

Assignment Operator

The assignment operator initializes or updates a value. An example can be:

val x = 4
var y = 3
y = x // y has the value of 4

Arithmetic Operators

Kotlin supports the standard arithmetic operators for all number types, namely, addition (+), subtraction (), multiplication (*) and division (/).

val i = 3
val j = 6
val addition = i + j // 9
val subtraction = j - i // 3
val multiplication = i * j // 18
val division = j.toDouble()/i.toDouble() // 2.0

The addition operator also supports String concatenation:

val hello = "Hello"
val world = "World"
val concatenation = hello + " " + world + "!"

Remainder Operator

The remainder operator (%) can be used to obtain the remainder value of a division:

val seven = 7
val two = 2
val remainder = seven % two // 1

Augmented Assignment Operator

Augmented Assignment Operators are the combination of assignment operator (=) with some other operator. An example with the addition assignment operator:

var x1 = 10 // x1 = 10
x1 += 9 // x1 = 19

The x1 += 9 is the short version of x1 = x1 + 9.

Equality and Inequality Operators

Equality and Inequality Operators return a Boolean value after evaluating a statement. These are:

  • (x == y), x is equal to (==y
  • (x != y), x is not equal to (!=y

Comparison Operators

Comparison Operators also return a Boolean value after evaluating a statement. These are:

  • (x > y), x is greater than (>y
  • (x < y), x is less than (<y
  • (x >= y), x is greater than or equal to (>=y
  • (x <= y), x is less than or equal to (<=y

Ranges

Ranges are shortcuts for expressing a range of values. There are four types of Ranges:

  • Range to (x..y)
  • Range downTo (y downTo x)
  • Range to with step (x..y step z)
  • Range until (x until y)
/*
 * Range to (x..y)
 */
 
for (i in 1..12) {
    println("$i x 2 = ${i * 2}")
}
 
/*
 * Range downTo (y downTo x)
 */
 
for (j in 12 downTo 1) {
    println("$j x 3 = ${j * 3}")
}

/*
 * Range to with step (x..y step z)
 */
 
for (i in 1..12 step 2) {
    println("$i x 4 = ${i * 4}")
}
 
/*
 * Range until (x until y)
 */
for (i in 1 until 13) {
    println("$i x 5 = ${i * 5}")
}

Negation Operator

The Negation Operator inverts a Boolean value from true to false or false to true:

var isSunny = false // isSunny has the value false
isSunny = !isSunny // isSunny has the value true

Lazy Conjunction Operator

The Lazy Conjunction Operator can be used when both values should be evaluated to true:

val isCorrectPin = true
val isCorrectFaceID = false
  
if (isCorrectPin && isCorrectFaceID) {
   println("Authentication successful!")
} else {
   println("Authentication failed!")
}

Lazy Disjunction Operator

The Lazy Disjunction Operator can be used when either one value evaluates to true:

if (isCorrectPin || isCorrectFaceID) {
   println("Authentication successful!")
} else {
   println("Authentication failed!")
}

Reference

https://kotlinlang.org/docs/reference/basic-types.html

https://kotlinlang.org/docs/reference/operator-overloading.html

Advertisement