I was looking over some mock OCJP questions. I came across a really baffling syntax. Here it is:
class OddStuff {
public static void main(String[] args) {
boolean b = false;
System.out.println((b != b));// False
System.out.println((b =! b));// True
}
}
Why does the output change between != and =!?
The question is just playing with you with confusing spacing.
b != bis the usual!=(not equals) comparison.On the other hand:
b =! bis better written asb = !bwhich is parsed as:Thus it’s two operators.
b.b.The assignment operator returns the assigned value. Therefore,
(b =! b)evaluates to true – which is what you print out.