Kotlin Assignment Operator
Kotlin Assignment Operator assigns the right operand, usually a value or an expression, to the left operand, usually a variable or constant.
Syntax
The syntax to use Assignment operator is
x = value
where
x
is the variable name=
is the symbol used for Assignment operatorvalue
is the value that we would like to assign to variablex
Examples
Assign variable message with the string value “Hello World”
In the following program, we take a variable: message
, and assign this variable with a value "Hello World"
, using Assignment operator.
Kotlin Program
fun main() {
var message = "Hello World"
println(message)
}
Output
Hello World
Assign a new value to a variable
We can also assign a new value to a variable using Assignment operator.
In the following program, we have initialised the variable message
with a value "Hello World"
. Then we assign a new value, say "Hello User"
to the message
variable using Assignment operator.
Kotlin Program
fun main() {
var message = "Hello World"
message = "Hello User"
println(message)
}
Output
Hello User