Difference between !(a==b) and a!=b

There is no difference in the semantics of the two expressions. I would say there is no good reason to ever write the former.

But now I am confused, because as a teacher I assume you know a lot of your subject, so could there be a good reason why she used her method over mine?

The best thing to do is to ask your teacher. The most important thing to remember about teachers - as human beings - is that they have experiences, prejudices and are fallible.

In other words, she might have been bitten by some problem in the past which was solved by writing it like this - ask her about it, you might learn something.

Or, she perhaps thinks it's better, and can't articulate a strong benefit over personal preference; or that was the way she learnt, and hasn't seen a strong need to change - you learn something from that, insofar as there is more than one way to articulate the same semantics.

(I have a strong preference for != because it's neater - fewer parentheses; and why would the language designers bother providing != if you weren't intended to use it - but these are my personal preferences).

Or, she maybe meant to use !=, and forgot to go back to fix it. It's easy to forget to clean things up.


In Java, there is no difference between a!=b and !(a==b). Choosing the later form is a stylistic choice, which, to be honest, most static analysis tools/IDEs will issue a warning about.


Your teacher is using other ways to write the if-statement.

It doesn't matter if you write

if(array[i] != null)

or

if(!(array[i] == null))

because ! defines as not in most programming language. In this situation both of them are equal.

In conclusion, you can choose whatever style you like.

Hope you understood and good luck!

Always ask your teacher when you wonder about something :)