This is allowed by the Java compiler, what is it doing?
int x = x = 1;
I realize that x is assigned to x, but how can it have two =
s?
This is allowed by the Java compiler, what is it doing?
int x = x = 1;
I realize that x is assigned to x, but how can it have two =
s?
x = 1 both assigns the value 1 to x and also 'returns' 1, it allows for things like this:
while ((line = reader.readLine()) != null)
Read assignment statement from right to left:
Acording to Assignment Operators
There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a.
So,
int x = x = 1;
is the same as
x = (x = 1);
then
x = 1; x = x;
int x
puts x
on the stack.
The right hand part x = 1
assigns 1 to x
. But this is an expression with value 1.
Finally this is re-assigned to x
.