Kotlin comes with a wide range of built - in operators that are used for common operations. These include arithmetic operators (+
, -
, *
, /
, %
), comparison operators (==
, !=
, <
, >
, <=
, >=
), logical operators (&&
, ||
, !
), and bitwise operators (and
, or
, xor
, shl
, shr
).
fun main() {
// Arithmetic operators
val a = 10
val b = 3
val sum = a + b
val difference = a - b
val product = a * b
val quotient = a / b
val remainder = a % b
println("Sum: $sum, Difference: $difference, Product: $product, Quotient: $quotient, Remainder: $remainder")
// Comparison operators
val isEqual = a == b
val isGreater = a > b
println("Is equal: $isEqual, Is greater: $isGreater")
// Logical operators
val c = true
val d = false
val andResult = c && d
val orResult = c || d
val notResult =!c
println("And result: $andResult, Or result: $orResult, Not result: $notResult")
// Bitwise operators
val num1 = 5 // binary: 0101
val num2 = 3 // binary: 0011
val bitwiseAnd = num1 and num2 // binary: 0001
println("Bitwise and: $bitwiseAnd")
}
One of the powerful features of Kotlin is the ability to overload operators. This means that you can define how an operator behaves for your own custom classes. To overload an operator, you need to define a member function or an extension function with a specific name corresponding to the operator.
class ComplexNumber(val real: Double, val imaginary: Double) {
// Overloading the plus operator
operator fun plus(other: ComplexNumber): ComplexNumber {
return ComplexNumber(this.real + other.real, this.imaginary + other.imaginary)
}
override fun toString(): String {
return "$real + ${imaginary}i"
}
}
fun main() {
val c1 = ComplexNumber(1.0, 2.0)
val c2 = ComplexNumber(3.0, 4.0)
val sum = c1 + c2
println("Sum of complex numbers: $sum")
}
Kotlin’s built - in arithmetic operators are used extensively in mathematical calculations. For example, in a financial application, you might use these operators to calculate interest, profit, or loss.
fun calculateInterest(principal: Double, rate: Double, time: Double): Double {
return principal * rate * time
}
fun main() {
val principal = 1000.0
val rate = 0.05
val time = 2.0
val interest = calculateInterest(principal, rate, time)
println("Interest: $interest")
}
Kotlin provides several operators for working with collections. The +
operator can be used to concatenate two collections, and the -
operator can be used to remove elements from a collection.
fun main() {
val list1 = listOf(1, 2, 3)
val list2 = listOf(4, 5, 6)
val combinedList = list1 + list2
println("Combined list: $combinedList")
val newList = combinedList - listOf(3, 4)
println("New list after removal: $newList")
}
Operator overloading is particularly useful when working with custom classes. For example, in a game development scenario, you might have a Vector2D
class representing a 2D vector, and you can overload the +
operator to add two vectors.
class Vector2D(val x: Double, val y: Double) {
operator fun plus(other: Vector2D): Vector2D {
return Vector2D(this.x + other.x, this.y + other.y)
}
override fun toString(): String {
return "($x, $y)"
}
}
fun main() {
val v1 = Vector2D(1.0, 2.0)
val v2 = Vector2D(3.0, 4.0)
val sumVector = v1 + v2
println("Sum of vectors: $sumVector")
}
+
operator for a custom class, it should perform an operation that is conceptually similar to addition.Kotlin operators are a powerful tool that can greatly enhance the expressiveness and readability of your code. By understanding the core concepts of built - in operators and operator overloading, and by applying them in typical usage scenarios following best practices, you can write more efficient and maintainable Kotlin code. Whether you are working on mathematical calculations, collection manipulation, or custom class operations, Kotlin operators have got you covered.