I just going through code someone has written and I saw |= usage, looking up on Java operators, it suggests bitwise or and assign operation, can anyone explain and give me an example of it?
Here is the code that read it:
for (String search : textSearch.getValue())
matches |= field.contains(search);
is the same as
It calculates the bitwise OR of the two operands, and assigns the result to the left operand.
To explain your example code:
I presume
matchesis aboolean; this means that the bitwise operators behave the same as logical operators.On each iteration of the loop, it
ORs the current value ofmatcheswith whatever is returned fromfield.contains(). This has the effect of setting it totrueif it was already true, or iffield.contains()returns true.So, it calculates if any of the calls to
field.contains(), throughout the entire loop, has returnedtrue.