Skip to main content
The = operator in Kotlin is the fundamental assignment operator. It binds the evaluated result of a right-hand side (RHS) expression to a left-hand side (LHS) identifier, establishing either an initial reference in memory or updating an existing mutable reference.

Technical Characteristics

Statement, Not an Expression Unlike languages such as Java or C, the = operator in Kotlin is a statement, not an expression. It does not evaluate to or return a value. Consequently, chained assignments are syntactically invalid and will result in a compilation error.
Mutability and Initialization The compiler enforces strict rules on the = operator based on the LHS declaration:
  • val (Read-only): The = operator acts strictly as an initializer. It can only be applied once to bind the initial reference. Subsequent applications result in a Val cannot be reassigned compilation error.
  • var (Mutable): The = operator acts as both an initializer and a reassignment operator, allowing the LHS identifier to point to new memory addresses or primitive values throughout its lifecycle.
Type Safety and Variance The Kotlin compiler requires that the evaluated type of the RHS expression be identical to, or a valid subtype of, the declared type of the LHS identifier. Implicit narrowing conversions via the = operator are prohibited.
Non-Overloadable The basic = operator cannot be overloaded. It is a hardcoded language construct. However, compound assignment operators that incorporate = (such as +=, -=, *=) can be overloaded by implementing their corresponding Assign functions (e.g., plusAssign, minusAssign) on the target class.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More