What is the difference between & and && in Java?

The question is just playing with you with confusing spacing.

b != b is the usual != (not equals) comparison.

On the other hand:

b =! b is better written as b = !b which is parsed as:

b = (!b)

Thus it's two operators.

  1. First invert b.
  2. Then assign it back to b.

The assignment operator returns the assigned value. Therefore, (b =! b) evaluates to true - which is what you print out.


b=!b is an assignment. It assigns b to !b and the expression evaluates to the resulting value, which is true.


b != b means ! (b == b): the opposite of b == b.

b =! b is actually b = !b, an assignment. It's toggling b's value. An assignment evaluates to the value of the expression, so this will evaluate to !b (along with having changed the value of b).