Operators are symbols that represent specific actions, such as arithmetic operations or logical comparisons, which can be performed on operands (values). Kotlin supports a wide range of operators that allow you to perform common tasks efficiently. This tutorial will cover various types of operators available in Kotlin, including arithmetic, comparison, assignment, and logical operators.
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
+): Adds two operands.-): Subtracts the second operand from the first.*): Multiplies two operands./): Divides the first operand by the second. The result type depends on the types of the operands:
%): Returns the remainder of dividing the first operand by the second.fun main() {
val a = 10
val b = 3
println("Addition: ${a + b}") // Output: Addition: 13
println("Subtraction: ${a - b}") // Output: Subtraction: 7
println("Multiplication: ${a * b}") // Output: Multiplication: 30
println("Division: ${a / b}") // Output: Division: 3 (integer division)
println("Modulus: ${a % b}") // Output: Modulus: 1
}
Kotlin does not have built-in increment (++) or decrement (--) operators. However, you can achieve similar functionality using the inc() and dec() functions.
fun main() {
var x = 5
println("x before increment: $x") // Output: x before increment: 5
x = x.inc()
println("x after increment: $x") // Output: x after increment: 6
x = x.dec()
println("x after decrement: $x") // Output: x after decrement: 5
}
Comparison operators are used to compare two values and return a Boolean result.
==): Checks if two operands are equal.!=): Checks if two operands are not equal.>): Checks if the first operand is greater than the second.<): Checks if the first operand is less than the second.>=): Checks if the first operand is greater than or equal to the second.<=): Checks if the first operand is less than or equal to the second.fun main() {
val a = 10
val b = 5
println("a == b: ${a == b}") // Output: a == b: false
println("a != b: ${a != b}") // Output: a != b: true
println("a > b: ${a > b}") // Output: a > b: true
println("a < b: ${a < b}") // Output: a < b: false
println("a >= b: ${a >= b}") // Output: a >= b: true
println("a <= b: ${a <= b}") // Output: a <= b: false
}
Assignment operators are used to assign values to variables. Kotlin supports compound assignment operators that combine arithmetic and assignment operations.
=): Assigns the value of the right operand to the left operand.+=, -=: Addition and subtraction*=: Multiplication/=: Division%=: Modulusfun main() {
var a = 10
println("a before += 5: $a") // Output: a before += 5: 10
a += 5
println("a after += 5: $a") // Output: a after += 5: 15
println("a before -= 3: $a") // Output: a before -= 3: 15
a -= 3
println("a after -= 3: $a") // Output: a after -= 3: 12
println("a before *= 4: $a") // Output: a before *= 4: 12
a *= 4
println("a after *= 4: $a") // Output: a after *= 4: 48
println("a before /= 6: $a") // Output: a before /= 6: 48
a /= 6
println("a after /= 6: $a") // Output: a after /= 6: 8
println("a before %= 3: $a") // Output: a before %= 3: 8
a %= 3
println("a after %= 3: $a") // Output: a after %= 3: 2
}
Logical operators are used to combine multiple conditions and return a Boolean result.
&&): Returns true if both operands are true.||): Returns true if at least one operand is true.!): Inverts the value of the operand (true becomes false, and vice versa).fun main() {
val a = true
val b = false
println("a && b: ${a && b}") // Output: a && b: false
println("a || b: ${a || b}") // Output: a || b: true
println("!a: ${!a}") // Output: !a: false
}
Bitwise operators operate on the binary representation of integers.
&): Performs a bitwise AND operation.|): Performs a bitwise OR operation.^): Performs a bitwise XOR (exclusive OR) operation.!): Inverts the bits of the operand.<<): Shifts the bits of the first operand to the left by the number of positions specified by the second operand.>>): Shifts the bits of the first operand to the right by the number of positions specified by the second operand.fun main() {
val a = 6 // Binary: 0110
val b = 3 // Binary: 0011
println("a & b: ${a and b}") // Output: a & b: 2 (Binary: 0010)
println("a | b: ${a or b}") // Output: a | b: 7 (Binary: 0111)
println("a ^ b: ${a xor b}") // Output: a ^ b: 5 (Binary: 0101)
println("!a: ${a.inv()}") // Output: !a: -7 (Binary: ...11111111111111111111111111111010)
println("a << 2: ${a shl 2}") // Output: a << 2: 24 (Binary: 11000)
println("a >> 1: ${a shr 1}") // Output: a >> 1: 3 (Binary: 00011)
}
Kotlin provides operators for working with ranges and collections.
..): Creates a range from the first operand to the second operand.until): Creates a range from the first operand up to but not including the second operand.in): Checks if an element is contained within a collection or range.fun main() {
val range = 1..5
println("Range: $range") // Output: Range: 1..5
val inclusiveRange = 1 until 5
println("Inclusive Range: $inclusiveRange") // Output: Inclusive Range: 1 until 5
val numbers = listOf(1, 2, 3, 4, 5)
println("Is 3 in numbers? ${3 in numbers}") // Output: Is 3 in numbers? true
}
Kotlin allows you to overload operators for custom classes. This means you can define how certain operators behave when used with instances of your class.
data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point): Point {
return Point(this.x + other.x, this.y + other.y)
}
}
fun main() {
val p1 = Point(1, 2)
val p2 = Point(3, 4)
val p3 = p1 + p2
println("p3: $p3") // Output: p3: Point(x=4, y=6)
}
Kotlin provides a rich set of operators that facilitate various operations in your applications. Understanding and effectively using these operators can lead to cleaner and more efficient code. By leveraging arithmetic, comparison, assignment, logical, bitwise, range, and collection operators, you can write expressive and concise Kotlin programs.
In the next section, we will explore control flow structures in Kotlin, allowing you to create more complex logic based on conditions and loops.