Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
Kotlin provides a rich set of operators that allow you to perform a variety of operations. In this tutorial, we'll provide an overview of the most commonly used operators in Kotlin.
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Remainder (Modulus)==
: Checks for structural equality!=
: Checks for structural inequality===
: Checks for referential equality (i.e., whether two references point to the same object)!==
: Checks for referential inequality<
: Less than<=
: Less than or equal to>
: Greater than>=
: Greater than or equal to=
: Assigns a value+=
: Adds and assigns-=
: Subtracts and assigns*=
: Multiplies and assigns/=
: Divides and assigns%=
: Takes modulus and assigns+
: Indicates a positive number (e.g., +3
)-
: Negates a number (e.g., -3
)++
: Increment--
: Decrement!
: Logical negation&&
: Logical AND||
: Logical OR!
: Logical NOTin
Operatorin
: Checks if a value is present in a range, collection, etc.!in
: Checks if a value is not presentis
OperatorUsed for type checking.
is
: Checks if an object is of a given type!is
: Checks if an object is not of a given typeInt
and Long
)shl
: Shifts leftshr
: Shifts rightushr
: Unsigned shift rightand
: Bitwise ANDor
: Bitwise ORxor
: Bitwise XORinv
: Bitwise inversion?:
(Elvis operator): Used for null safety (e.g., val length = name?.length ?: 0
)?.
(Safe call operator): Performs the call only if the receiver is non-null.!!
(Non-null assertion operator): Throws a NullPointerException
if the value is null.::
(Reflection operator): References a class, function, or property.// Arithmetic val sum = 5 + 3 // Comparison val isEqual = (sum == 8) // Assignment var number = 10 number += 5 // Unary val negation = -number // Logical val result = (sum > 0) && (number > 0) // 'in' operator val withinRange = 5 in 1..10 // 'is' operator val isString = "Hello" is String // Bitwise val shifted = number shl 2
Kotlin offers a wide range of operators for various operations, making the code expressive and concise. Proper understanding and use of these operators can enhance the clarity and efficiency of your Kotlin code.
Arithmetic operators in Kotlin:
+, -, *, /, %
.val sum = 5 + 3 val difference = 5 - 3 val product = 5 * 3 val quotient = 5 / 3 val remainder = 5 % 3
Comparison operators in Kotlin:
==, !=, >, <, >=, <=
.val isEqual = (5 == 3) val isNotEqual = (5 != 3) val isGreaterThan = (5 > 3) val isLessThan = (5 < 3)
Logical operators in Kotlin:
&& (and), || (or), ! (not)
.val andResult = true && false val orResult = true || false val notResult = !true
Bitwise operators in Kotlin:
and, or, xor, shl, shr, ushr
.val bitwiseAnd = 5 and 3 val bitwiseOr = 5 or 3 val bitwiseXor = 5 xor 3 val leftShift = 5 shl 1 val rightShift = 5 shr 1
Assignment operators in Kotlin:
=, +=, -=, *=, /=, %=
.var num = 5 num += 3 num -= 2 num *= 4 num /= 2 num %= 3
Unary operators in Kotlin:
+, -
.val positiveNum = +5 val negativeNum = -5
Overloading operators in Kotlin:
data class Complex(val real: Double, val imag: Double) { operator fun plus(other: Complex) = Complex(real + other.real, imag + other.imag) }
Custom operators in Kotlin:
data class Quantity(val value: Int) { operator fun times(other: Quantity) = Quantity(value * other.value) }
Operator precedence and associativity in Kotlin:
val result = 5 + 3 * 2 // Result is 11, considering precedence
Using operators with different data types in Kotlin:
val result = 5 + 3.0 // Result is 8.0, Int is promoted to Double
Null safety and operators in Kotlin:
val nullableInt: Int? = null val result = nullableInt?.plus(5) // Result is null if nullableInt is null