What's the point of the compound comparison operators?

I have read that b1 |= b2 is the shortcut equivalent to b1 = b1 | b2 b1 = b1 | b2 . My question is really two-fold:

Isn't "=" an assignment operator rather than a comparison operator? If so, what does it do in this context?

Or, is it a comparison operator here and what scenario exists where you would need to compare a variable to itself (ie b1 = b1 )?

(I realize this is probably the newbie-est newb question to ask, but I've only got informal experience writing bash scripts and no educational background in programming. Be kind. ;)

Note: this is in reference to the question here: Shortcut "or-assignment" (|=) operator in Java


There are many operators in Java. But 'Compound comparison operator' is not one of them. You should read Java basics from a good book like 'Head first Java'.

To answer this particular question, b1 |= b2 is compound assignment.

  • = assigns the the result of b1|b2 to LHS operand ie b1 .
  • Since it is clear now that it is an assignment operator not comparison, the result of b1 |= b1 will be same as b1 = b1|b1 .
  • (Note | here is the logical OR between two numbers not || which is a conditional operator. | and || have different meanings)

    HTH.

    链接地址: http://www.djcxy.com/p/73924.html

    上一篇: 考虑到最佳实践,我们可以使用双重一元运算符! 在JavaScript中?

    下一篇: 复合比较运算符有什么意义?