Java | = operator question

I need help with this weird statement |= . Can you explain to me what this code does?

 @Override public boolean addAll(Collection<? extends E> c) { boolean result = false; for (E e : c) { result |= add(e); } return result; } 
+7
java operators
source share
5 answers

All Collection members are added to the code using the add() method, which returns a boolean , indicating whether add() was executed or not. What the addAll method addAll is return true if any of the added successes and false if all of them failed. (This seems strange to me, as I would return true if all the additions were successful, but I got distracted.)

So you can do it like this:

 @Override public boolean addAll(Collection<? extends E> c) { boolean result = false; for (E e : c) { if (add(e)) { result = true; } } return result; } 

But this is a little more detailed, since you can act more directly with the result variable:

 @Override public boolean addAll(Collection<? extends E> c) { boolean result = false; for (E e : c) { result = add(e) || result; } return result; } 

So, we logically have the OR-old value of result with the return value of add to get the new value. (Note: we want the result be on the right side of || because || "shortcircuits" and does not interfere with checking the right side || if the left side is true ). So if add(e) and result were the other way around, this would not evaluate the right side - that is, if it did not run the add() method - once the result was true .)

The person who wrote this method decided that they want to be as concise as possible so that they change:

 result = add(e) || result; 

in

 result |= add(e); 

which matches with:

 result = result | add(e); 

Operator | is a bitwise OR that is not a logical OR, with the exception of Boolean, where the effect is basically equal to The same, the only difference is that | does not have the short circuit behavior mentioned above.

There is no ||= syntax in Java, so a bitwise OR is used, although even if it did, it would probably have the same squirrel cage problem mentioned above.

+2
source share

This is a shorthand for:

 result = result | add(e); 

Where | - bitwise OR operator.

+6
source share

this is a reduction for result = result | add (e). A pipe is a bitwise or operator.

+2
source share

This is a bitwise OR-IN result and add(e) and assigning it back to the result. Shorthand notation instead of writing result = result | add(e) result = result | add(e)

0
source share

The or-assign ( |= ) statement sets the variable on the LHS to the value that previously contained OR-ed, with the result of the RHS evaluation. For boolean types (as in this example), it modifies the variable so that it contains true when the value is true (and otherwise does not have a pure effect). This is not a short circuit rating.

The overall effect of the method is to call the method of the current add object for each element of the collection of arguments and to return true if any of these calls to add returns true (i.e. if something is really added, under reasonable assumptions about the value of the result add .. .)

0
source share

All Articles