开发者

Java |= operator question [duplicate]

开发者 https://www.devze.com 2023-01-17 07:11 出处:网络
This question already has answers here: What does "|=" mean? (pipe equal operator) (6 answers)
This question already has answers here: What does "|=" mean? (pipe equal operator) (6 answers) Closed 7 years ago.

I need help about this strange operator |=. Can you explain to me what this code does ?

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


It is a shorthand for:

result = result | add(e);

Where the | is the bitwise OR operator.


The code is adding all members of a Collection using the add() method which returns a boolean, indicating if the add() succeeded or not. What the addAll method does is return true if any of the adds succeeded and false if all of them failed. (This does seems odd to me, since I'd only return true if all the adds were succesful, but I digress.)

So you could 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 that's a little verbose as you can act on the result variable more directly:

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

So we're logically OR-ing the old value of result with the return value of add to get the new value. (Note - we want result to be on the right hand side of the ||; this is because || "shortcircuits" and doesn't bother checking the right hand side of an || if the left side is true). So if add(e) and result were the other way around it would not evaluate the right hand side - i.e. not run the add() method - once result were true.)

Whoever wrote that method decide they wanted to be as terse as possible so they changed:

result = add(e) || result;

to:

result |= add(e);

which is the same as:

result = result | add(e);

The | operator is a bitwise OR which is not the same a logical OR, except for booleans where the effect is basically the same, the only difference being the | does not have the shortcircuit behaviour mentioned above.

There is no ||= syntax in Java which is why the bitwise OR is being used, although even if it did it would probably have the same shortcircuit problem mentioned above.


it's shorthand for result = result | add(e). The pipe is the bitwise or operator.


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


The or-assign operator (|=) sets the variable on the LHS to the value it previously contained OR-ed with the result of evaluating the RHS. For boolean types (as in this example) it changes the variable to contain true when the value is true (and otherwise has no net effect). It does not short-circuit evaluate.

The overall effect of the method is to call the current object's add method for each element of the argument collection, and to return true if any of those calls to add return true (i.e., if anything actually got added, under reasonable assumptions about the meaning of the result of add…)

0

精彩评论

暂无评论...
验证码 换一张
取 消