Search⌘ K
AI Features

Operators: Unary, Increment, and Decrement

Explore unary operators in Kotlin, such as plus, minus, and logical not, and understand how increment and decrement operators modify values efficiently. Learn how to use pre- and post-increment/decrement, and discover operator overloading to extend these operations for custom object behavior to make your code more readable and functional.

Unary prefix operators

A +, -, or ! ...

Expression

Translation

+a

a.unaryPlus()

-a

a.unaryMinus()

!a

a.not()

Here is an example of overloading the unaryMinus operator.

Kotlin 1.5
data class Point(val x: Int, val y: Int)
operator fun Point.unaryMinus() = Point(-x, -y)
fun main() {
val point = Point(10, 20)
println(-point) // Point(x=-10, y=-20)
}

Increment and decrement operators

...