So..
= is an assignment, and == is a mapping, and this is always the case, regardless of where they are used.
And the destination is different from the "announcement." The assignment operator has a return value, but the declaration does not. Therefore, you cannot write boolean a = false in () the if statement, but you can write a = false when it was previously announced.
Not all jobs are legal. For instance:
int index; if (index = str.indexOf("something")) { ... }
This is not legal because String.indexOf(String) returns int, and if requires a boolean.
In addition, there is a huge difference between "legal" and "meaning ."
int index; if ((index = str.indexOf("something")) != -1) { ... }
This is legal because the != Operation returns a boolean, and makes sense since I want to check if the string contains the substring "something";
but
int index; boolean flag; if ( flag = ((index = str.indexOf("something")) != -1) ) { ... }
also legal, since the last operator returns boolean; but that does NOT make sense, because the != operator already returns a boolean.
source share