Kotlin

Kotlin Operators with Examples

Characters or unique letters known as operators in programming languages carry out specified actions on operands (variables or values). Programmers can use them to modify the data, perform computations, compare the numbers, and manage the program flow. In Kotlin, we use the operators that are designed to give the users with a clear, expressive way to execute different operations on the data. Operators make the code easier to comprehend, simplify the routine tasks, and increase the overall effectiveness of programming languages. Today’s guide will discuss all the available operators in Kotlin, along with each category they belong to, with the help of some code examples.

Arithmetic Operators

Arithmetic operators are employed to execute mathematical operations on numerical quantities in Kotlin. They provide you with the ability to mix and alter the numbers in different ways.

In this illustration, we will discuss a code example that covers all arithmetic operators at once. In the given code example, the operators are used to perform various operations on numeric values and manipulate the variables. The code starts with the main function that is declared by the “fun” keyword while it initializes two variables, v1 and v2, using the “val” keyword. The addition operator “+” is used to add the values of v1 and v2 and the result is assigned to another variable which is “add.” It calculates the sum of v1 and v2 which is 19.

The subtraction operator “-” is used to subtract the value of v2 from v1, and the result is allocated to the “sub” variable which is 5. The multiplication operator “*” multiplies the values of v1 and v2, and the result which is “84” is assigned to the “mul” variable. The division operator “/” is here to divide the value of v1 by v2 which results in “1”, and is assigned to the “quot” variable. The modulo operator “%” is cast-off to calculate the remnant when v1 is divided by v2. The result is 5 which is allocated to the “mod” variable.

The increment operator ++ is utilized to increase the mutable “I” value by 1. Primarily, the value of “I” becomes 9. The decrement operator — is castoff to decrement the mutable “I” value by 1. Afterward, using the decrement operator i–, the value of “I” becomes 8 again.

fun main() {
  val v1 = 12
  val v2 = 7
  val add = v1 + v2
  println("Sum: $add")
  val sub = v1 - v2
  println("Difference: $sub")
  val mul = v1 * v2
  println("Product: $mul")
  val quot = v1 / v2
  println("Quotient: $quot")
  val mod = v1 % v2
  println("Remainder: $mod")
  var i = 8
  i++
  println("Increment i by 1: $i")
  i--
  println("Decrement i by 1: $i")
}

 

The result of each mathematical expression using the operators is displayed on the console using the println() function statement after every calculation which is shown in the following:

Assignment Operators

Assignment operators in Kotlin are symbols to combine an arithmetic operation with the assignment operator when you want to perform an operation on a mutable and store the result back into the same variable without explicitly reassigning it.

The following code performs the various assignment operations on the “x” variable. It starts with the declaration of the main function and a mutable variable “x”; the value of x is 10. Declare another mutable “v” and “y” and initialize it with the value of 5. The first expression uses the addition assignment operator (+=) to add the value of “y” to the present the value of “x” and assigns the result back to “x”.

The next line utilizes the subtraction assignment operator (-=) to subtract “y” from “x” and returns the result to “x”. The next line uses the multiplication assignment operator (*=) to multiply the value of “x” by “y” and add the result back to “x”. The second last expression utilizes the division assignment operator (/=) to divide “x” by “y” and returns the result to “x”. The last expression uses the modulo assignment operator (%=) to get the remainder of dividing “x” by “y” and saves the result back to “x”.

fun main(args : Array<String>){
  var x = 10
  var y = 5
  x += y
  println(x)
  x -= y
  println(x)
  x *= y
  println(x)
  x /= y
  println(x)
  x %= y
  println(x)
}

 

The println() statement prints the value of “x” after using all assignment operators.

Comparison Operators

In this example, we apply all comparison operators to 2 variables in a single code. First, we declare two mutables, ” a” and” b”, with 11 and 5.

The first expression uses the equality comparison operator (==) to compare the values of “a” and” b” and allocates the comparison result (true or false) to the variable “eq”. The inequality comparison operator (!=) checks if” a” and” b” are not equal and saves the result to the variable “ne”. The line gt =a>b utilizes the greater than operator (>) to check if “a” is greater than” b” by assigning the result to the variable “gt”.

The less-than-comparison operator (<) finds out if “a” is smaller than “b”. The second last line has the greater than or equal to comparison operator (>=) to check if “a” is greater than or equal to “b” and saves the result to the variable “geq”. The last expression utilizes the less than or equal to comparison operator (<=) to check if “a” is less than or equal to” b”. It allocates the outcome of the assessment to the variable “leq”.

fun main() {
  val a = 11
  val b = 5
  val eq = a == b
  println("isEqual: $eq")
  val ne = a != b
  println("isNotEqual: $ne")
  val gt = a > b
  println("isGreater: $gt")
  val ls = a < b
  println("isLess: $ls")
  val geq = a >= b
  println("isGreaterOrEqual: $geq")
  val leq = a <= b
  println("isLessOrEqual: $leq")
}

 

After all comparisons, we print their results one by one using the println() function statements.

Logical Operators

The code example evaluates the different logical expressions based on the given values of “a”, “b”, and “c”, i.e. 9, 2, and 5, and stores the results in Boolean variables “r1”, “r2”, and “r3”.

The first line uses the logical AND operator (&&) to check if both conditions (a > b) and (a < c) are true. The very next line casts off the logical OR operator (||) to check if at least one of the conditions (a > b) or (a > c) is true. The last expression uses the logical NOT operator (!) to negate the condition (a < b).

fun main() {
  val a = 9
  val b = 2
  val c = 5
  val r1 = (a > b) && (a < c)
  println("Output 1: $r1")
  val r2 = (a > b) || (a > c)
  println("Output 2: $r2")
  val r3 = !(a < b)
  println("Output 3: $r3")
}

 

The println statement after each expression prints the results of these logical operations which allow you to observe the logical outcomes based on the specified conditions.

Conclusion

Programmers don’t need to create sophisticated logic from the start to control variables, compare the values, make judgments, or do computations. Instead, they may do all of these things by utilizing the operators. This guide briefly explained the use of each logical operator in Kotlin language with the help of code examples.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.