The Assignment operator

Note that the test in that last code example is performed with the == operator and not the assignment operator =. Sooner or later everyone tries to compile code with a test like this

//do something

This simply assigns the value of 1 to the variable i, rather than testing the condition. Luckily Java will flag this type of error at compile time. Some languages (notably Visual Basic) use the = operator for both assignment and for testing equality. As these are quite different acts it seems reasonable to use different operators. Note that assignment works from right to left. You cannot assign in the way you might in algebra as

int x=2;
int y=0;
/* incorrect, will generate a compile time error!*/
x * 2 = y;
Last modified: Thursday, 24 July 2014, 2:54 PM