TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-- operator in Kotlin is the unary decrement operator, used to subtract one from the value of a mutable variable. Under the hood, it acts as syntactic sugar for the dec() operator function, meaning the expression a-- or --a is translated by the compiler to a = a.dec().
The operator can be applied in two distinct positions relative to the operand, which dictates the evaluation order when used within a larger expression:
- Prefix (
--a): The decrement operation is evaluated before the expression resolves. It subtracts one from the variable and returns the newly decremented value. - Postfix (
a--): The decrement operation is evaluated after the expression resolves. It returns the original value of the variable, and then subtracts one from the underlying variable in memory.
Operator Overloading and Custom Types
Because Kotlin utilizes an operator overloading convention, the-- operator is not restricted to built-in numeric primitives. It can be applied to any custom class, provided the following conditions are met:
- The variable being decremented is mutable (declared as
var), because the operator performs a reassignment. - The type implements a
dec()member function or extension function. - The
dec()function is prefixed with theoperatorkeyword. - The
dec()function returns a value of the same type (or a subtype) as the caller.
Thread Safety
The-- operator is not atomic. The translation to a = a.dec() involves three distinct steps: reading the current value, invoking the decrement logic, and writing the new value back to the variable. In a multithreaded environment, concurrent use of the -- operator on shared variables requires explicit synchronization or the use of atomic classes (e.g., AtomicInteger.decrementAndGet()).
Master Kotlin with Deep Grasping Methodology!Learn More





