Given the following code snippet:
int i = 0;
int y = + ++i;
System.out.println(y);
The result is 1. Why is this a valid declaration? Can anyone explain what is =+?
int y = + ++i;
The first +
in this line is simply the unary +
operator (see: Assignment, Arithmetic, and Unary Operators). It does nothing. It's similar to the unary -
operator. The line above is equivalent to:
int y = ++i;
which increments i
and then assigns the new value of i
to y
.