Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
identifier = expression

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.
var a: Int
var b: Int
// a = b = 5 // Compilation error: Assignments are not expressions
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.
val immutableReference: String
immutableReference = "Initialized" // Valid: First assignment (Initialization)
// immutableReference = "Updated"  // Invalid: Reassignment of val

var mutableReference: Int = 1      // Valid: Initialization
mutableReference = 2               // Valid: Reassignment
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.
var number: Number
number = 42       // Valid: Int is a subtype of Number
number = 3.14     // Valid: Double is a subtype of Number

var integer: Int
// integer = number // Invalid: Number is not a subtype of Int
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.
Master Kotlin with Deep Grasping Methodology!Learn More