Operators in Kotlin

In Kotlin, operators are special symbols or keywords that allow you to perform operations on variables and values. Kotlin supports various types of operators, including arithmetic operators, assignment operators, comparison operators, logical operators, and bitwise operators.

Here’s a brief overview of each type of operator:

Arithmetic Operators:

These operators are used to perform arithmetic operations on numeric values. Kotlin supports standard arithmetic operators such as + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).

Addition (+): The addition operator adds two values together.

val num1 = 10
val num2 = 20
val sum = num1 + num2
println("The sum of $num1 and $num2 is $sum")

Output: The sum of 10 and 20 is 30

Subtraction (-): The subtraction operator subtracts one value from another.

val num1 = 20
val num2 = 10
val diff = num1 - num2
println("The difference between $num1 and $num2 is $diff")

Output: The difference between 20 and 10 is 10

Multiplication (*): The multiplication operator multiplies two values together.

val num1 = 5
val num2 = 7
val product = num1 * num2
println("The product of $num1 and $num2 is $product")

Output: The product of 5 and 7 is 35

Division (/): The division operator divides one value by another.

val num1 = 20
val num2 = 5
val quotient = num1 / num2
println("The quotient of $num1 and $num2 is $quotient")

Output: The quotient of 20 and 5 is 4

Modulo (%): The modulo operator returns the remainder of a division operation.

val num1 = 20
val num2 = 6
val remainder = num1 % num2
println("The remainder of $num1 divided by $num2 is $remainder")

Output: The remainder of 20 divided by 6 is 2

These are the basic arithmetic operators in Kotlin. You can use them with any numeric type, including integers and floating-point numbers.

Assignment Operators:

These operators are used to assign values to variables. The most common assignment operator is the = operator, which assigns the value on the right-hand side to the variable on the left-hand side.

Simple assignment (=): The simple assignment operator assigns the value on the right-hand side to the variable on the left-hand side.

var num = 10
num = 20
println("The value of num is $num")

Output: The value of num is 20

Compound assignment (+=, -=, *=, /=, %=): The compound assignment operators combine a simple assignment with an arithmetic operation.

var num = 10
num += 5 // equivalent to num = num + 5
println("The value of num is $num")

num -= 3 // equivalent to num = num - 3
println("The value of num is $num")

num *= 2 // equivalent to num = num * 2
println("The value of num is $num")

num /= 4 // equivalent to num = num / 4
println("The value of num is $num")

num %= 3 // equivalent to num = num % 3
println("The value of num is $num")

Output:

The value of num is 15
The value of num is 12
The value of num is 24
The value of num is 6
The value of num is 0

Increment and decrement (++ and –): The increment and decrement operators increase or decrease the value of a variable by 1.

var num = 10
num++ // equivalent to num = num + 1
println("The value of num is $num")

num-- // equivalent to num = num - 1
println("The value of num is $num")

Output:

The value of num is 11
The value of num is 10

These are the basic assignment operators in Kotlin. You can use them with any variable of the appropriate type. The compound assignment operators can be especially useful for performing arithmetic operations and updating a variable’s value in a single step.

Comparison Operators:

These operators are used to compare two values and return a Boolean result. Kotlin supports standard comparison operators such as == (equality), != (inequality), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

Equal to (==): The equal to operator checks whether two values are equal.

val num1 = 10
val num2 = 10
val equal = num1 == num2
println("Are $num1 and $num2 equal? $equal")

val str1 = "hello"
val str2 = "world"
val equal2 = str1 == str2
println("Are $str1 and $str2 equal? $equal2")

Output:

Are 10 and 10 equal? true
Are hello and world equal? false

Not equal to (!=): The not equal to operator checks whether two values are not equal.

val num1 = 10
val num2 = 20
val notEqual = num1 != num2
println("Are $num1 and $num2 not equal? $notEqual")

val str1 = "hello"
val str2 = "hello"
val notEqual2 = str1 != str2
println("Are $str1 and $str2 not equal? $notEqual2")

Output:

Are 10 and 20 not equal? true
Are hello and hello not equal? false

Greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=): These operators compare two values and return a Boolean value indicating whether the comparison is true or false.

val num1 = 10
val num2 = 20
val greater = num1 > num2
val greaterOrEqual = num1 >= num2
val less = num1 < num2
val lessOrEqual = num1 <= num2
println("$num1 is greater than $num2: $greater")
println("$num1 is greater than or equal to $num2: $greaterOrEqual")
println("$num1 is less than $num2: $less")
println("$num1 is less than or equal to $num2: $lessOrEqual")

Output:

10 is greater than 20: false
10 is greater than or equal to 20: false
10 is less than 20: true
10 is less than or equal to 20: true

These are the basic comparison operators in Kotlin. You can use them with any values of the appropriate type. They are commonly used in conditional statements to make decisions based on whether certain conditions are true or false.

Logical Operators:

These operators are used to perform logical operations on Boolean values. Kotlin supports standard logical operators such as && (logical and), || (logical or), and ! (logical not).

AND (&&): The AND operator returns true only if both operands are true.

val num1 = 10
val num2 = 20
val num3 = 30
val result = num1 < num2 && num2 < num3
println("Is $num1 less than $num2 AND $num2 less than $num3? $result")

Output:

Is 10 less than 20 AND 20 less than 30? true

OR (||): The OR operator returns true if at least one of the operands is true.

val num1 = 10
val num2 = 20
val num3 = 30
val result = num1 > num2 || num2 < num3
println("Is $num1 greater than $num2 OR $num2 less than $num3? $result")

Output:

Is 10 greater than 20 OR 20 less than 30? true

NOT (!): The NOT operator returns the opposite Boolean value of its operand.

val num1 = 10
val num2 = 20
val result1 = num1 < num2
val result2 = !result1
println("Is $num1 less than $num2? $result1")
println("Is $num1 not less than $num2? $result2")

Output:

Is 10 less than 20? true
Is 10 not less than 20? false

These are the basic logical operators in Kotlin. You can use them with any Boolean values or expressions. They are commonly used in conditional statements and loops to make decisions based on multiple conditions.

Bitwise Operators:

These operators are used to perform bitwise operations on integer values. Kotlin supports standard bitwise operators such as and (bitwise and), or (bitwise or), xor (bitwise exclusive or), shl (left shift), and shr (right shift).

In addition to these standard operators, Kotlin also supports overloading operators, which allows you to define custom operators for your own types. This can make your code more concise and expressive.