NenTenEn NenTenEn - 4 years ago 77
Java Question

What's the point of the compound comparison operators?

I have read that

b1 |= b2
is the shortcut equivalent to
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 (i.e.
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

Answer Source

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.

  1. = assigns the the result of b1|b2 to LHS operand i.e b1.
  2. 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download